-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added email verification while creating user and sending email (#1208)
- Loading branch information
Showing
5 changed files
with
167 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...ver/appsmith-server/src/test/java/com/appsmith/server/controllers/UserControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.appsmith.server.controllers; | ||
|
||
import com.appsmith.server.configurations.SecurityTestConfig; | ||
import com.appsmith.server.services.SessionUserService; | ||
import com.appsmith.server.services.UserOrganizationService; | ||
import com.appsmith.server.services.UserService; | ||
import com.appsmith.server.solutions.UserSignup; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.test.context.support.WithMockUser; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.test.web.reactive.server.WebTestClient; | ||
import org.springframework.web.reactive.function.BodyInserters; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
@RunWith(SpringRunner.class) | ||
@WebFluxTest(UserController.class) | ||
@Import(SecurityTestConfig.class) | ||
public class UserControllerTest { | ||
@Autowired | ||
private WebTestClient webTestClient; | ||
|
||
@MockBean | ||
private UserService userService; | ||
|
||
@MockBean | ||
private SessionUserService sessionUserService; | ||
|
||
@MockBean | ||
private UserOrganizationService userOrganizationService; | ||
|
||
@MockBean | ||
private UserSignup userSignup; | ||
|
||
@Test | ||
@WithMockUser | ||
public void createUserWithInvalidEmailAddress() { | ||
List<String> invalidAddresses = Arrays.asList( | ||
"plainaddress", | ||
"#@%^%#$@#$@#.com", | ||
"@example.com", | ||
"Joe Smith <[email protected]>", | ||
"email.example.com", | ||
"email@[email protected]", | ||
"[email protected]", | ||
"[email protected]", | ||
"[email protected]", | ||
"[email protected] (Joe Smith)", | ||
"[email protected]", | ||
"[email protected]", | ||
"[email protected]" | ||
); | ||
for (String invalidAddress : invalidAddresses) { | ||
try { | ||
webTestClient.post().uri("/api/v1/users"). | ||
contentType(MediaType.APPLICATION_JSON). | ||
body(BodyInserters.fromValue(String.format("{\"name\":\"test-name\"," + | ||
"\"email\":\"%s\",\"password\":\"test-password\"}", invalidAddress))). | ||
exchange(). | ||
expectStatus().isEqualTo(400). | ||
expectBody().json("{\n" + | ||
" \"responseMeta\": {\n" + | ||
" \"status\": 400,\n" + | ||
" \"success\": false,\n" + | ||
" \"error\": {\n" + | ||
" \"code\": 4028,\n" + | ||
" \"message\": \"Validation Failure(s): {email=must be a well-formed email address}\"\n" + | ||
" }\n" + | ||
" }\n" + | ||
"}"); | ||
} catch (Throwable exc) { | ||
System.out.println("******************************"); | ||
System.out.println(String.format("Failed for >>> %s", invalidAddress)); | ||
System.out.println("******************************"); | ||
throw exc; | ||
} | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...rver/appsmith-server/src/test/java/com/appsmith/server/notifications/EmailSenderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.appsmith.server.notifications; | ||
|
||
import com.appsmith.server.configurations.EmailConfig; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static org.mockito.Mockito.doReturn; | ||
import static org.mockito.Mockito.verifyNoInteractions; | ||
|
||
@Slf4j | ||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
@DirtiesContext | ||
public class EmailSenderTest { | ||
@MockBean | ||
private JavaMailSender javaMailSender; | ||
|
||
@MockBean | ||
private EmailConfig emailConfig; | ||
|
||
@Autowired | ||
private EmailSender emailSender; | ||
|
||
@Test | ||
public void itShouldNotSendMailsWithInvalidAddresses() { | ||
//noinspection ResultOfMethodCallIgnored | ||
doReturn(true).when(emailConfig).isEmailEnabled(); | ||
List<String> invalidAddresses = Arrays.asList( | ||
"plainaddress", | ||
"#@%^%#$@#$@#.com", | ||
"@example.com", | ||
"Joe Smith <[email protected]>", | ||
"email.example.com", | ||
"email@[email protected]", | ||
"[email protected]", | ||
"[email protected]", | ||
"[email protected]", | ||
"[email protected] (Joe Smith)", | ||
"[email protected]", | ||
"[email protected]", | ||
"[email protected]" | ||
); | ||
|
||
for (String invalidAddress : invalidAddresses) { | ||
try { | ||
emailSender.sendMail(invalidAddress, "test-subject", "email/welcomeUserTemplate.html", Collections.emptyMap()).block(); | ||
|
||
verifyNoInteractions(javaMailSender); | ||
} catch (Throwable exc) { | ||
System.out.println("******************************"); | ||
System.out.println(String.format("Failed for >>> %s", invalidAddress)); | ||
System.out.println("******************************"); | ||
throw exc; | ||
} | ||
} | ||
} | ||
} |