@@ -7,6 +7,7 @@ import org.mockito.BDDMockito._
7
7
import org .scalatest .mock .MockitoSugar
8
8
import org .scalatestplus .play ._
9
9
import persistence .UserRepository
10
+ import play .api .libs .json .Json
10
11
import play .api .mvc ._
11
12
import play .api .test .Helpers ._
12
13
import play .api .test ._
@@ -16,30 +17,38 @@ class AuthControllerSpec extends PlaySpec with MockitoSugar with Results {
16
17
val authController = new AuthController (userRepository)
17
18
18
19
" An auth controller" should {
19
- // TODO: no mocking: for using "with session" we need a running application
20
- // "authenticate with valid credentials" in {
21
- // val user = "user"
22
- // val pass = "pass"
23
- // given(userRepository.login(user, pass)) willReturn true
24
- // val request = FakeRequest(POST, "/auth").withHeaders("Authorization" -> authHeaderValue(s"$user:$pass"))
25
- // val result = authController.authenticate()(request)
26
- // status(result) mustBe OK
27
- // val bodyText: String = contentAsString(result)
28
- // bodyText mustBe "logged"
29
- // }
30
20
31
21
" not authenticate with not matching credentials" in {
22
+ // given
32
23
val user = " user"
33
24
val pass = " pass"
25
+ given (userRepository.login(user, pass)) willReturn None
34
26
val request = FakeRequest (POST , " /auth" ).withHeaders(" Authorization" -> authHeaderValue(s " $user: $pass" ))
27
+
28
+ // when
35
29
val result = authController.authenticate()(request)
30
+
31
+ // then
36
32
status(result) mustBe UNAUTHORIZED
37
33
}
38
34
39
- " not authenticate with not valid authentication header" in {
35
+ " authenticate with matching credentials" in {
36
+ // given
40
37
val user = " user"
41
38
val pass = " pass"
42
- val request = FakeRequest (POST , " /auth" ).withHeaders(" Authorization" -> s " $user: $pass" )
39
+ given (userRepository.login(user, pass)) willReturn Some (" token" )
40
+ val request = FakeRequest (POST , " /auth" ).withHeaders(" Authorization" -> authHeaderValue(s " $user: $pass" ))
41
+
42
+ // when
43
+ val result = authController.authenticate()(request)
44
+
45
+ // then
46
+ status(result) mustBe OK
47
+ contentAsJson(result) mustBe Json .obj(" token" -> " token" )
48
+ }
49
+
50
+ " not authenticate with not valid authentication header" in {
51
+ val request = FakeRequest (POST , " /auth" ).withHeaders(" Authorization" -> " user:pass" )
43
52
val result = authController.authenticate()(request)
44
53
status(result) mustBe UNAUTHORIZED
45
54
}
@@ -54,22 +63,15 @@ class AuthControllerSpec extends PlaySpec with MockitoSugar with Results {
54
63
}
55
64
56
65
" return an empty user from non encoded credentials" in {
57
- val user = " user"
58
- val pass = " pass"
59
- val request = FakeRequest (POST , " /auth" ).withHeaders(" Authorization" -> s " $user: $pass" )
60
-
66
+ val request = FakeRequest (POST , " /auth" ).withHeaders(" Authorization" -> " user:pass" )
61
67
authController.credentials(request) mustBe None
62
68
}
63
69
64
70
" return an empty user from invalid encoded credentials" in {
65
- val user = " user"
66
- val pass = " pass"
67
- val request = FakeRequest (POST , " /auth" ).withHeaders(" Authorization" -> authHeaderValue(s " $user: $pass:test " ))
68
-
71
+ val request = FakeRequest (POST , " /auth" ).withHeaders(" Authorization" -> authHeaderValue(" user:pass:test" ))
69
72
authController.credentials(request) mustBe None
70
73
}
71
74
}
72
75
73
76
def authHeaderValue (credentials : String ) = " Basic " + Base64 .getEncoder.encodeToString(credentials.getBytes)
74
-
75
77
}
0 commit comments