-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthControllerTest.java
60 lines (43 loc) · 2.12 KB
/
AuthControllerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package se.mow_e.controllers;
import com.jayway.jsonpath.JsonPath;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.provisioning.UserDetailsManager;
import se.mow_e.services.JwtService;
@SpringBootTest(properties = "spring.datasource.url=jdbc:h2:mem:test")
@AutoConfigureMockMvc
public class AuthControllerTest {
@Autowired
RequestTokenConfig requestTokenConfig;
@Autowired
private JwtService jwtService;
@Autowired
private UserDetailsManager detailsManager;
@Test
public void loginEndpointShouldReturnAToken() throws Exception {
String response = requestTokenConfig.authRequest("admin", "pass",
HttpMethod.POST, "/auth/login", HttpStatus.OK.value());
Assertions.assertEquals(JsonPath.read(response, "$.status"), "successful");
Authentication authentication = jwtService.getAuthentication(requestTokenConfig.getToken(response));
Assertions.assertTrue(authentication.getAuthorities().stream().anyMatch(a -> "ROLE_ADMIN".equals(a.getAuthority())));
}
@Test
void signUpEndpointDoNotRegisterExistingUsername() throws Exception {
String response = requestTokenConfig.authRequest("admin", "pass",
HttpMethod.POST, "/auth/signup", HttpStatus.BAD_REQUEST.value());
Assertions.assertEquals(JsonPath.read(response, "$.status"), "error");
}
@Test
void signUpEndpointRegisterNewUser() throws Exception {
String response = requestTokenConfig.authRequest("user3", "pass",
HttpMethod.POST, "/auth/signup", HttpStatus.OK.value());
Assertions.assertEquals(JsonPath.read(response, "$.status"), "successful");
Assertions.assertTrue(detailsManager.userExists("user3"));
}
}