| @@ -0,0 +1,48 @@ | ||
| package test.budget.integration | ||
|
|
||
| import java.util.UUID | ||
| import org.scalatest._ | ||
| import org.scalatestplus.play._ | ||
| import play.api.test._ | ||
| import play.api.test.Helpers._ | ||
| import play.api.libs.json._ | ||
|
|
||
| import models.budget.Projection | ||
| import models.budget.JsonImplicits._ | ||
| import test.budget.integration.util.IntegrationHelper | ||
|
|
||
| class ProjectionsSpec | ||
| extends PlaySpec | ||
| with OneServerPerSuite | ||
| with IntegrationHelper | ||
| { | ||
|
|
||
| "POST /projections" must { | ||
|
|
||
| val json = Json.obj( | ||
| "name" -> "My Projection" | ||
| ) | ||
|
|
||
| "return a 401 Unauthorized if the request isn't authenticated" in { | ||
| val response = await { unauthentictedRequest("/projections").post(json) } | ||
| response.status mustBe UNAUTHORIZED | ||
| } | ||
|
|
||
| "return a 409 Conflict any required fields aren't specified" in { | ||
| Seq("name").foreach { field => | ||
| val response = await { authenticatedRequest("/projections").post(json - field) } | ||
| response.status mustBe CONFLICT | ||
| } | ||
| } | ||
|
|
||
| "return a new Account if authorized and valid" in { | ||
| val response = await { authenticatedRequest("/projections").post(json) } | ||
| response.status mustBe CREATED | ||
|
|
||
| val account = response.json.as[Projection] | ||
| account.name mustBe "My Projection" | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,45 @@ | ||
| package test.budget.integration.util | ||
|
|
||
| import java.util.UUID | ||
| import play.api.libs.ws.WS | ||
| import org.scalatestplus.play.OneServerPerSuite | ||
|
|
||
| import io.useless.accesstoken.AccessToken | ||
| import io.useless.account.User | ||
| import io.useless.client.accesstoken.{AccessTokenClient, MockAccessTokenClient} | ||
| import io.useless.client.account.{AccountClient, MockAccountClient} | ||
|
|
||
| object IntegrationHelper { | ||
|
|
||
| val accessToken = AccessToken( | ||
| guid = UUID.fromString("00000000-0000-0000-0000-000000000000"), | ||
| resourceOwner = User( | ||
| guid = UUID.fromString("00000000-1111-1111-1111-111111111111"), | ||
| handle = "khy", | ||
| name = None | ||
| ), | ||
| client = None, | ||
| scopes = Seq() | ||
| ) | ||
|
|
||
| } | ||
|
|
||
| trait IntegrationHelper { | ||
|
|
||
| self: OneServerPerSuite => | ||
|
|
||
| val mockAccessTokenClient = new MockAccessTokenClient(Seq(IntegrationHelper.accessToken)) | ||
| val mockAccountClient = new MockAccountClient(Seq(IntegrationHelper.accessToken.resourceOwner)) | ||
|
|
||
| AccessTokenClient.setMock(mockAccessTokenClient) | ||
| AccountClient.setMock(mockAccountClient) | ||
|
|
||
| def authenticatedRequest(path: String) = { | ||
| unauthentictedRequest(path).withHeaders(("Authorization" -> IntegrationHelper.accessToken.guid.toString)) | ||
| } | ||
|
|
||
| def unauthentictedRequest(path: String) = { | ||
| WS.url(s"http://localhost:$port$path") | ||
| } | ||
|
|
||
| } |