Search in sources :

Example 11 with User

use of com.sanctionco.thunder.models.User in project thunder by RohanNagar.

the class MongoDbUsersDaoTest method testSuccessfulInsert.

@Test
void testSuccessfulInsert() {
    MongoCollection<Document> collection = mock(MongoCollection.class);
    MongoDbUsersDao usersDao = new MongoDbUsersDao(collection, MAPPER);
    User result = usersDao.insert(USER).join();
    // Insert will set the creation and update time
    long creationTime = (Long) result.getProperties().get("creationTime");
    long updateTime = (Long) result.getProperties().get("lastUpdateTime");
    assertAll("The creation and update time are correct", () -> assertTrue(creationTime > CURR_TIME), () -> assertTrue(updateTime > CURR_TIME), () -> assertEquals(creationTime, updateTime));
    assertEquals(USER.withTime(creationTime, updateTime), result);
    verify(collection, times(1)).insertOne(argThat((Document doc) -> doc.containsKey("_id") && doc.containsKey("id") && doc.containsKey("version") && doc.containsKey("creation_time") && doc.containsKey("update_time") && doc.containsKey("document")));
}
Also used : User(com.sanctionco.thunder.models.User) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Test(org.junit.jupiter.api.Test)

Example 12 with User

use of com.sanctionco.thunder.models.User in project thunder by RohanNagar.

the class MongoDbUsersDaoTest method testSameExistingEmail.

@Test
void testSameExistingEmail() {
    MongoCollection<Document> collection = mock(MongoCollection.class);
    FindIterable<Document> findIterable = mock(FindIterable.class);
    when(findIterable.first()).thenReturn(DOCUMENT);
    doReturn(findIterable).when(collection).find(any(Bson.class));
    MongoDbUsersDao usersDao = new MongoDbUsersDao(collection, MAPPER);
    User result = usersDao.update("test@test.com", USER).join();
    // Update will set the update time
    long creationTime = (Long) result.getProperties().get("creationTime");
    long updateTime = (Long) result.getProperties().get("lastUpdateTime");
    assertAll("The creation and update time are correct", () -> assertEquals(CURR_TIME, creationTime), () -> assertTrue(updateTime > CURR_TIME));
    assertEquals(USER.withTime(creationTime, updateTime), result);
    verify(collection, times(1)).find(eq(Filters.eq("_id", "test@test.com")));
    verify(collection, times(1)).updateOne(argThat((Bson bson) -> {
        BsonDocument doc = bson.toBsonDocument(BsonDocument.class, MongoClientSettings.getDefaultCodecRegistry());
        return doc.containsKey("version");
    }), any(Bson.class));
    verify(collection, never()).deleteOne(any());
}
Also used : User(com.sanctionco.thunder.models.User) BsonDocument(org.bson.BsonDocument) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Bson(org.bson.conversions.Bson) Test(org.junit.jupiter.api.Test)

Example 13 with User

use of com.sanctionco.thunder.models.User in project thunder by RohanNagar.

the class RequestValidatorTest method testValidateUserNullEmailAddress.

@Test
void testValidateUserNullEmailAddress() {
    Email email = new Email(null, false, "token");
    User user = new User(email, "password");
    RequestValidationException e = assertThrows(RequestValidationException.class, () -> validator.validate(user));
    assertEquals("Invalid email address format. Please try again.", e.getMessage());
    assertEquals(RequestValidationException.Error.INVALID_PARAMETERS, e.getError());
}
Also used : Email(com.sanctionco.thunder.models.Email) User(com.sanctionco.thunder.models.User) Test(org.junit.jupiter.api.Test)

Example 14 with User

use of com.sanctionco.thunder.models.User in project thunder by RohanNagar.

the class ThunderClientFake method sendVerificationEmail.

@Override
public CompletableFuture<User> sendVerificationEmail(String email, String password) {
    // This does not actually send an email, but it does update the verification token of the user.
    var user = inMemoryStore.get(email);
    if (user == null) {
        return fail(404);
    }
    if (requirePasswordHeader && !user.getPassword().equals(password)) {
        return fail(401);
    }
    var updated = new User(new Email(user.getEmail().getAddress(), user.getEmail().isVerified(), UUID.randomUUID().toString()), user.getPassword(), user.getProperties());
    inMemoryStore.put(email, updated);
    return CompletableFuture.completedFuture(updated);
}
Also used : User(com.sanctionco.thunder.models.User) Email(com.sanctionco.thunder.models.Email)

Example 15 with User

use of com.sanctionco.thunder.models.User in project thunder by RohanNagar.

the class ThunderClientTest method testUpdateUser.

@Test
void testUpdateUser() throws Exception {
    User response = client.updateUser(user, "email", password).get();
    assertEquals(user.getEmail(), response.getEmail());
}
Also used : User(com.sanctionco.thunder.models.User) Test(org.junit.jupiter.api.Test)

Aggregations

User (com.sanctionco.thunder.models.User)51 Test (org.junit.jupiter.api.Test)41 Email (com.sanctionco.thunder.models.Email)26 RequestValidator (com.sanctionco.thunder.validation.RequestValidator)11 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 UsersDao (com.sanctionco.thunder.dao.UsersDao)8 Objects (java.util.Objects)8 Logger (org.slf4j.Logger)8 LoggerFactory (org.slf4j.LoggerFactory)8 UUID (java.util.UUID)7 BsonDocument (org.bson.BsonDocument)6 Document (org.bson.Document)6 Metered (com.codahale.metrics.annotation.Metered)5 Counter (com.codahale.metrics.Counter)4 MetricRegistry (com.codahale.metrics.MetricRegistry)4 ThunderException (com.sanctionco.thunder.ThunderException)4 SwaggerAnnotations (com.sanctionco.thunder.openapi.SwaggerAnnotations)4 MetricNameUtil (com.sanctionco.thunder.util.MetricNameUtil)4 RequestValidationException (com.sanctionco.thunder.validation.RequestValidationException)4 POST (javax.ws.rs.POST)4