|
| 1 | +package integration.api.ingredients |
| 2 | + |
| 3 | +import api.Authentication.AuthenticatedUser |
| 4 | +import api.ingredients.CreateIngredientReqBody |
| 5 | +import db.repositories.IngredientsRepo |
| 6 | +import integration.common.Utils.* |
| 7 | +import integration.common.ZIOIntegrationTestSpec |
| 8 | + |
| 9 | +import io.circe.generic.auto.* |
| 10 | +import zio.http.* |
| 11 | +import zio.* |
| 12 | +import zio.test.* |
| 13 | + |
| 14 | +object CreateIngredientTests extends ZIOIntegrationTestSpec: |
| 15 | + private def endpointPath: URL = |
| 16 | + URL(Path.root / "ingredients") |
| 17 | + |
| 18 | + private def createIngredient(user: AuthenticatedUser, reqBody: CreateIngredientReqBody): |
| 19 | + RIO[Client, Response] = |
| 20 | + Client.batched( |
| 21 | + post(endpointPath) |
| 22 | + .withJsonBody(reqBody) |
| 23 | + .addAuthorization(user) |
| 24 | + ) |
| 25 | + |
| 26 | + override def spec: Spec[TestEnvironment & Scope, Any] = suite("Create invitation tests")( |
| 27 | + test("When unauthorized should get 401") { |
| 28 | + for |
| 29 | + resp <- Client.batched(post(endpointPath)) |
| 30 | + yield assertTrue(resp.status == Status.Unauthorized) |
| 31 | + }, |
| 32 | + test("When create ingredient should get 204 and custom ingredient should be created"){ |
| 33 | + for |
| 34 | + user <- registerUser |
| 35 | + ingredientName <- randomString |
| 36 | + |
| 37 | + resp <- createIngredient(user, CreateIngredientReqBody(ingredientName)) |
| 38 | + |
| 39 | + bodyStr <- resp.body.asString |
| 40 | + ingredientId <- ZIO.fromOption(bodyStr.toUUID) |
| 41 | + .orElse(failed(Cause.fail(s"Could not parse response ingredientId $bodyStr"))) |
| 42 | + |
| 43 | + ingredient <- ZIO.serviceWithZIO[IngredientsRepo](_ |
| 44 | + .get(ingredientId) |
| 45 | + .provideUser(user) |
| 46 | + ) |
| 47 | + yield assertTrue(resp.status == Status.Created) |
| 48 | + && assertTrue(ingredient.is(_.some).ownerId.is(_.some) == user.userId) |
| 49 | + && assertTrue(ingredient.is(_.some).name == ingredientName) |
| 50 | + }, |
| 51 | + ).provideLayer(testLayer) |
0 commit comments