Steffen Nitschke
4 years ago
8 changed files with 216 additions and 4 deletions
-
16Multi-Chess.xml
-
12build.gradle
-
9fh.fd.ci.server/build.gradle
-
46fh.fd.ci.server/src/main/java/de/fd/fh/server/access/Access.java
-
58fh.fd.ci.server/src/main/java/de/fd/fh/server/access/AccessRepository.java
-
6fh.fd.ci.server/src/main/java/de/fd/fh/server/access/Role.java
-
42fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessRepositoryTest.java
-
31fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessTokenTest.java
@ -0,0 +1,16 @@ |
|||
<code_scheme name="Multi-Chess" version="173"> |
|||
<option name="FORMATTER_TAGS_ENABLED" value="true" /> |
|||
<codeStyleSettings language="JAVA"> |
|||
<option name="BRACE_STYLE" value="2" /> |
|||
<option name="CLASS_BRACE_STYLE" value="2" /> |
|||
<option name="METHOD_BRACE_STYLE" value="2" /> |
|||
<option name="LAMBDA_BRACE_STYLE" value="2" /> |
|||
<option name="ELSE_ON_NEW_LINE" value="true" /> |
|||
<option name="CATCH_ON_NEW_LINE" value="true" /> |
|||
<option name="FINALLY_ON_NEW_LINE" value="true" /> |
|||
<option name="IF_BRACE_FORCE" value="3" /> |
|||
<option name="DOWHILE_BRACE_FORCE" value="3" /> |
|||
<option name="WHILE_BRACE_FORCE" value="3" /> |
|||
<option name="FOR_BRACE_FORCE" value="3" /> |
|||
</codeStyleSettings> |
|||
</code_scheme> |
@ -0,0 +1,46 @@ |
|||
package de.fd.fh.server.access; |
|||
|
|||
import de.fd.fh.server.user.UserId; |
|||
import dev.morphia.annotations.Embedded; |
|||
import dev.morphia.annotations.Entity; |
|||
import dev.morphia.annotations.Id; |
|||
import lombok.AllArgsConstructor; |
|||
import lombok.Getter; |
|||
import lombok.NoArgsConstructor; |
|||
|
|||
@Entity("login") |
|||
@NoArgsConstructor |
|||
@AllArgsConstructor |
|||
@Getter |
|||
public class Access |
|||
{ |
|||
@Id |
|||
private String _id; |
|||
|
|||
private String name; |
|||
|
|||
private String password; |
|||
|
|||
@Embedded |
|||
private UserId userId; |
|||
|
|||
@Embedded |
|||
private AccessToken token; |
|||
|
|||
private Role role; |
|||
|
|||
void removeToken() |
|||
{ |
|||
this.token = null; |
|||
} |
|||
|
|||
void setToken(final AccessToken token) |
|||
{ |
|||
this.token = token; |
|||
} |
|||
|
|||
void updatePassword(final String newPassword) |
|||
{ |
|||
this.password = newPassword; |
|||
} |
|||
} |
@ -0,0 +1,58 @@ |
|||
package de.fd.fh.server.access; |
|||
|
|||
import com.mongodb.MongoClient; |
|||
import com.mongodb.WriteResult; |
|||
import de.fd.fh.server.user.UserId; |
|||
import dev.morphia.Datastore; |
|||
import dev.morphia.Key; |
|||
import dev.morphia.Morphia; |
|||
|
|||
public class AccessRepository |
|||
{ |
|||
private final Datastore datastore; |
|||
|
|||
public AccessRepository() |
|||
{ |
|||
System.out.println("AccessRepo"); |
|||
final Morphia morphia = new Morphia(); |
|||
|
|||
morphia.mapPackage("de.fd.fh.server.access"); |
|||
|
|||
this.datastore = morphia.createDatastore(new MongoClient(), "smartwarfare"); |
|||
datastore.ensureIndexes(); |
|||
} |
|||
|
|||
AccessRepository(Datastore datastore) |
|||
{ |
|||
this.datastore = datastore; |
|||
} |
|||
|
|||
public Key<Access> save(final Access access) |
|||
{ |
|||
return datastore.save(access); |
|||
} |
|||
|
|||
Access findByUserName(final String name) |
|||
{ |
|||
return datastore.createQuery(Access.class) |
|||
.field("name").equal(name).first(); |
|||
} |
|||
|
|||
Access findByToken(final String token) |
|||
{ |
|||
return datastore.createQuery(Access.class) |
|||
.field("token.token").equal(token).first(); |
|||
} |
|||
|
|||
Access findByUserId(final UserId userId) |
|||
{ |
|||
return datastore.createQuery(Access.class) |
|||
.field("userId.identifier").equal(userId.getIdentifier()).first(); |
|||
} |
|||
|
|||
WriteResult deleteLoginByUserId(final UserId userId) |
|||
{ |
|||
return datastore.delete(datastore.createQuery(Access.class) |
|||
.field("userId.identifier").equal(userId.getIdentifier()).first()); |
|||
} |
|||
} |
@ -0,0 +1,6 @@ |
|||
package de.fd.fh.server.access; |
|||
|
|||
public enum Role |
|||
{ |
|||
ADMIN, USER |
|||
} |
@ -0,0 +1,42 @@ |
|||
package de.fd.fh.server.access; |
|||
|
|||
import de.fd.fh.server.user.UserId; |
|||
import dev.morphia.Datastore; |
|||
import dev.morphia.Key; |
|||
import org.junit.jupiter.api.BeforeEach; |
|||
import org.junit.jupiter.api.Test; |
|||
import org.mockito.Mock; |
|||
|
|||
import static org.hamcrest.MatcherAssert.assertThat; |
|||
import static org.hamcrest.Matchers.notNullValue; |
|||
import static org.mockito.ArgumentMatchers.any; |
|||
import static org.mockito.BDDMockito.then; |
|||
import static org.mockito.Mockito.mock; |
|||
import static org.mockito.Mockito.when; |
|||
|
|||
class AccessRepositoryTest |
|||
{ |
|||
@Mock |
|||
private Datastore datastore; |
|||
|
|||
@BeforeEach |
|||
public void before() |
|||
{ |
|||
datastore = mock(Datastore.class); |
|||
} |
|||
|
|||
@Test |
|||
void given_newUser_when_saveUser_should_storeUserInDatabase() |
|||
{ |
|||
when(datastore.save(any(Access.class))) |
|||
.thenReturn(new Key<>(Access.class, "collection", "id")); |
|||
|
|||
final Access access = new Access("testId", "testName", "testPwd", UserId.of("userId"), |
|||
null, Role.USER); |
|||
|
|||
final Key<Access> result = new AccessRepository(datastore).save(access); |
|||
|
|||
assertThat("Key is null", result, notNullValue()); |
|||
then(datastore).should().save(any(Access.class)); |
|||
} |
|||
} |
@ -0,0 +1,31 @@ |
|||
package de.fd.fh.server.access; |
|||
|
|||
import de.fd.fh.server.user.UserId; |
|||
import org.junit.jupiter.api.Test; |
|||
|
|||
import static org.hamcrest.MatcherAssert.assertThat; |
|||
import static org.hamcrest.Matchers.equalTo; |
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
|
|||
class AccessTokenTest |
|||
{ |
|||
@Test |
|||
void given_accessData_when_createAccessToken_should_createGeneratedToken() |
|||
{ |
|||
final Access access = new Access( |
|||
"testId", |
|||
"testName", |
|||
"testPwd", |
|||
UserId.of("12345"), |
|||
null, |
|||
Role.USER); |
|||
|
|||
final AccessToken result = AccessToken.of(access); |
|||
|
|||
assertNotNull(result.getCreatedDate()); |
|||
assertNotNull(result.getToken()); |
|||
assertThat(result.getToken().length(), equalTo(64)); |
|||
assertEquals(result.getRole(), Role.USER); |
|||
assertEquals(result.getUserId(), UserId.of("12345")); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue