Skip to content

Commit 0689a6b

Browse files
committed
test fixes I
1 parent 282b527 commit 0689a6b

File tree

3 files changed

+25
-31
lines changed

3 files changed

+25
-31
lines changed

test/controllers/AuthControllerSpec.scala

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.mockito.BDDMockito._
77
import org.scalatest.mock.MockitoSugar
88
import org.scalatestplus.play._
99
import persistence.UserRepository
10+
import play.api.libs.json.Json
1011
import play.api.mvc._
1112
import play.api.test.Helpers._
1213
import play.api.test._
@@ -16,30 +17,38 @@ class AuthControllerSpec extends PlaySpec with MockitoSugar with Results {
1617
val authController = new AuthController(userRepository)
1718

1819
"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-
// }
3020

3121
"not authenticate with not matching credentials" in {
22+
// given
3223
val user = "user"
3324
val pass = "pass"
25+
given(userRepository.login(user, pass)) willReturn None
3426
val request = FakeRequest(POST, "/auth").withHeaders("Authorization" -> authHeaderValue(s"$user:$pass"))
27+
28+
// when
3529
val result = authController.authenticate()(request)
30+
31+
// then
3632
status(result) mustBe UNAUTHORIZED
3733
}
3834

39-
"not authenticate with not valid authentication header" in {
35+
"authenticate with matching credentials" in {
36+
// given
4037
val user = "user"
4138
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")
4352
val result = authController.authenticate()(request)
4453
status(result) mustBe UNAUTHORIZED
4554
}
@@ -54,22 +63,15 @@ class AuthControllerSpec extends PlaySpec with MockitoSugar with Results {
5463
}
5564

5665
"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")
6167
authController.credentials(request) mustBe None
6268
}
6369

6470
"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"))
6972
authController.credentials(request) mustBe None
7073
}
7174
}
7275

7376
def authHeaderValue(credentials: String) = "Basic " + Base64.getEncoder.encodeToString(credentials.getBytes)
74-
7577
}

test/persistence/UserRepositorySpec.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ class UserRepositorySpec extends PlaySpecification with EmbeddedMongo {
2828
val userRepository = app.injector.instanceOf[UserRepository]
2929

3030
// then
31-
val result = userRepository.login("user", "pass")
32-
result must beTrue
31+
userRepository.login("user", "pass").isDefined must beTrue
3332
}
3433
}
3534
}

test/services/WordServiceImplSpec.scala

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,4 @@ class WordServiceImplSpec extends FlatSpec with Matchers with MockitoSugar {
2929
val resp = wordService.saveWord(f)
3030
resp.value.get.get.json shouldBe json
3131
}
32-
33-
it should "adapt the retrieved text" in {
34-
wordService.adaptText("abc") shouldBe "abc"
35-
wordService.adaptText("abc ") shouldBe "abc"
36-
wordService.adaptText("ab cd ") shouldBe "ab cd"
37-
}
38-
3932
}

0 commit comments

Comments
 (0)