|
| 1 | +package org.dukecon; |
| 2 | + |
| 3 | +import io.restassured.http.ContentType; |
| 4 | +import io.restassured.response.ValidatableResponse; |
| 5 | +import org.dukecon.support.BaseTests; |
| 6 | +import org.dukecon.support.TokenGatherer; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; |
| 9 | +import org.springframework.restdocs.restassured3.RestDocumentationFilter; |
| 10 | + |
| 11 | +import java.util.UUID; |
| 12 | + |
| 13 | +import static io.restassured.RestAssured.given; |
| 14 | +import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath; |
| 15 | +import static org.hamcrest.Matchers.emptyString; |
| 16 | +import static org.hamcrest.Matchers.equalTo; |
| 17 | +import static org.springframework.restdocs.restassured3.RestAssuredRestDocumentation.document; |
| 18 | + |
| 19 | +public class EventsBookingTests extends BaseTests { |
| 20 | + |
| 21 | + private final String badToken = UUID.randomUUID().toString(); |
| 22 | + private final String userToken = new TokenGatherer().gatherUserToken(); |
| 23 | + |
| 24 | + private final String eventId = UUID.randomUUID().toString(); //TODO get date from conference? |
| 25 | + |
| 26 | + private final String pathToEventsBooking = System.getProperty("dukecon.apitests.pathToEventsBooking"); |
| 27 | + private final String pathToEventsBookingEvent = String.format("%s/%s",pathToEventsBooking,eventId); |
| 28 | + @Test |
| 29 | + public void testEventsBookingGet() { |
| 30 | + whenUrlOkAndContentTypeMatches(pathToEventsBooking, ContentType.JSON.toString(), document("eventsBookingGet")) |
| 31 | + .assertThat() |
| 32 | + .statusCode(200) |
| 33 | + .body(matchesJsonSchemaInClasspath("schemas/eventsBooking.json")); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testEventsBookingGetAuthenticated() { |
| 38 | + whenAuthenticatedAndContentTypeMatches(userToken,pathToEventsBooking, ContentType.JSON.toString(), document("eventsBookingGetWithAuth")) |
| 39 | + .assertThat() |
| 40 | + .statusCode(200) |
| 41 | + .and() |
| 42 | + .body(matchesJsonSchemaInClasspath("schemas/eventsBooking.json")); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + @EnabledIfSystemProperty(named = "dukecon.apitests.auth.enabled", matches = "true") |
| 47 | + public void testEventBookingsUpdateIsInGeneralSecured() { |
| 48 | + String eventId = UUID.randomUUID().toString(); // TODO get date from conference? |
| 49 | + |
| 50 | + eventsBookingPost(badToken, "{\"fullyBooked\":false,\"numberOccupied\":\"10\"}", |
| 51 | + document("eventsBookingPostWithBadAuthCreate")) |
| 52 | + .assertThat() |
| 53 | + //TODO this should return 401/403 |
| 54 | + .statusCode(201) |
| 55 | + .body(equalTo("{\"numberOccupied\":10,\"fullyBooked\":false}")); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + @EnabledIfSystemProperty(named = "dukecon.apitests.auth.enabled", matches = "true") |
| 60 | + public void testEventBookingsUpdateIsAdminSecured() { |
| 61 | + eventsBookingPost(userToken, "{\"fullyBooked\":false,\"numberOccupied\":\"10\"}", |
| 62 | + document("eventsBookingPostWithUserAuthCreate")) |
| 63 | + .assertThat() |
| 64 | + //TODO this should return 401/403 |
| 65 | + .statusCode(201) |
| 66 | + .body(equalTo("{\"numberOccupied\":10,\"fullyBooked\":false}")); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testEventBookingsUpdate() { |
| 71 | + String eventId = UUID.randomUUID().toString(); //TODO get date from conference? |
| 72 | + |
| 73 | + eventsBookingPost(userToken,"{\"fullyBooked\":true,\"numberOccupied\":\"11\"}", document("eventsBookingPostWithAuthCreate")) |
| 74 | + .assertThat() |
| 75 | + .statusCode(201) |
| 76 | + .body(equalTo("{\"numberOccupied\":11,\"fullyBooked\":true}")); |
| 77 | + |
| 78 | + //TODO verify that the created eventbooking exists in the conference? |
| 79 | + |
| 80 | + //TODO check that updating an existing eventBooking works |
| 81 | + eventsBookingPost(userToken,"{\"fullyBooked\":false,\"numberOccupied\":\"10\"}", document("eventsBookingPostWithAuthUpdate")) |
| 82 | + .assertThat() |
| 83 | + .statusCode(204) |
| 84 | + .body(emptyString()); |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + //TODO limit overflow? |
| 89 | + //TODO crosscheck with feedback? |
| 90 | + private ValidatableResponse eventsBookingPost(String token, String eventsBookingContent, RestDocumentationFilter documentationFilter) { |
| 91 | + return given(this.spec).auth().oauth2(token) |
| 92 | + .body(eventsBookingContent) |
| 93 | + .contentType(ContentType.JSON) |
| 94 | + .filter(documentationFilter) |
| 95 | + .post(pathToEventsBookingEvent) |
| 96 | + .then(); |
| 97 | + } |
| 98 | +} |
0 commit comments