| 
 | 1 | +package com.serenitydojo.playwright.toolshop.login;  | 
 | 2 | + | 
 | 3 | +import com.google.gson.Gson;  | 
 | 4 | +import com.google.gson.JsonObject;  | 
 | 5 | +import com.microsoft.playwright.APIRequest;  | 
 | 6 | +import com.microsoft.playwright.APIRequestContext;  | 
 | 7 | +import com.microsoft.playwright.APIResponse;  | 
 | 8 | +import com.microsoft.playwright.Playwright;  | 
 | 9 | +import com.microsoft.playwright.junit.UsePlaywright;  | 
 | 10 | +import com.microsoft.playwright.options.RequestOptions;  | 
 | 11 | +import com.serenitydojo.playwright.toolshop.domain.User;  | 
 | 12 | +import org.junit.jupiter.api.AfterEach;  | 
 | 13 | +import org.junit.jupiter.api.BeforeEach;  | 
 | 14 | +import org.junit.jupiter.api.Test;  | 
 | 15 | +import org.junit.jupiter.params.ParameterizedTest;  | 
 | 16 | +import org.junit.jupiter.params.provider.CsvSource;  | 
 | 17 | + | 
 | 18 | +import static org.assertj.core.api.Assertions.assertThat;  | 
 | 19 | +import static org.assertj.core.api.SoftAssertions.assertSoftly;  | 
 | 20 | + | 
 | 21 | +@UsePlaywright  | 
 | 22 | +public class RegisterUserAPITest {  | 
 | 23 | + | 
 | 24 | +    private APIRequestContext request;  | 
 | 25 | +    private Gson gson = new Gson();  | 
 | 26 | + | 
 | 27 | +    @BeforeEach  | 
 | 28 | +    void setup(Playwright playwright) {  | 
 | 29 | +        request = playwright.request().newContext(  | 
 | 30 | +                new APIRequest.NewContextOptions()  | 
 | 31 | +                        .setBaseURL("https://api.practicesoftwaretesting.com")  | 
 | 32 | +        );  | 
 | 33 | +    }  | 
 | 34 | + | 
 | 35 | +    @AfterEach  | 
 | 36 | +    void tearDown() {  | 
 | 37 | +        if (request != null) {  | 
 | 38 | +            request.dispose();  | 
 | 39 | +        }  | 
 | 40 | +    }  | 
 | 41 | + | 
 | 42 | +    @Test  | 
 | 43 | +    void should_register_user() {  | 
 | 44 | +        User validUser = User.randomUser();  | 
 | 45 | + | 
 | 46 | +        var response = request.post("/users/register",  | 
 | 47 | +                RequestOptions.create()  | 
 | 48 | +                        .setHeader("Content-Type", "application/json")  | 
 | 49 | +                        .setData(validUser)  | 
 | 50 | +        );  | 
 | 51 | + | 
 | 52 | +        String responseBody = response.text();  | 
 | 53 | +        User createdUser = gson.fromJson(responseBody, User.class);  | 
 | 54 | + | 
 | 55 | +        JsonObject responseObject = gson.fromJson(responseBody, JsonObject.class);  | 
 | 56 | + | 
 | 57 | +        assertSoftly(softly -> {  | 
 | 58 | +            softly.assertThat(response.status())  | 
 | 59 | +                    .as("Registration should return 201 created status code")  | 
 | 60 | +                    .isEqualTo(201);  | 
 | 61 | + | 
 | 62 | +            softly.assertThat(createdUser)  | 
 | 63 | +                    .as("Created user should match the specified user without the password")  | 
 | 64 | +                    .isEqualTo(validUser.withPassword(null));  | 
 | 65 | + | 
 | 66 | +            assertThat(responseObject.has("password"))  | 
 | 67 | +                    .as("No password should be returned")  | 
 | 68 | +                    .isFalse();  | 
 | 69 | + | 
 | 70 | +            softly.assertThat(responseObject.get("id").getAsString())  | 
 | 71 | +                    .as("Registered user should have an id")  | 
 | 72 | +                    .isNotEmpty();  | 
 | 73 | + | 
 | 74 | +            softly.assertThat(  | 
 | 75 | +                    response.headers().get("content-type")  | 
 | 76 | +            ).contains("application/json");  | 
 | 77 | +        });  | 
 | 78 | +    }  | 
 | 79 | + | 
 | 80 | +    @Test  | 
 | 81 | +    void first_name_is_mandatory() {  | 
 | 82 | +        User userWithNoName = new User(  | 
 | 83 | +                null,  | 
 | 84 | +                "Smith",  | 
 | 85 | +                "Some street",  | 
 | 86 | +                "Some city",  | 
 | 87 | +                "Some state",  | 
 | 88 | +                "Some country",  | 
 | 89 | +                "123445",  | 
 | 90 | +                "12345678899",  | 
 | 91 | +                "1979-01-01",  | 
 | 92 | +                "ABC123!£$%",  | 
 | 93 | + | 
 | 94 | +        );  | 
 | 95 | + | 
 | 96 | +        var response = request.post("/users/register",  | 
 | 97 | +                RequestOptions.create()  | 
 | 98 | +                        .setHeader("Content-Type", "application/json")  | 
 | 99 | +                        .setData(userWithNoName)  | 
 | 100 | +        );  | 
 | 101 | + | 
 | 102 | +        assertSoftly(softly -> {  | 
 | 103 | +            softly.assertThat(response.status()).isEqualTo(422);  | 
 | 104 | + | 
 | 105 | +            JsonObject responseObject = gson.fromJson(response.text(), JsonObject.class);  | 
 | 106 | + | 
 | 107 | +            softly.assertThat(responseObject.has("first_name")).isTrue();  | 
 | 108 | + | 
 | 109 | +            String errorMessage = responseObject.get("first_name").getAsString();  | 
 | 110 | + | 
 | 111 | +            softly.assertThat(errorMessage).isEqualTo("The first name field is required.");  | 
 | 112 | +        });  | 
 | 113 | + | 
 | 114 | +    }  | 
 | 115 | +    @ParameterizedTest(name = "Password ''{0}'' should fail with error: {1}")  | 
 | 116 | +    @CsvSource(delimiter = '|', quoteCharacter = '"', textBlock = """  | 
 | 117 | +            ''                         | The password field must be at least 8 characters.  | 
 | 118 | +            short                      | The password field must be at least 8 characters.  | 
 | 119 | +            longerpassword             | The password field must contain at least one uppercase and one lowercase letter.  | 
 | 120 | +            LONGERPASSWORD             | The password field must contain at least one uppercase and one lowercase letter.  | 
 | 121 | +            LongerPassword             | The password field must contain at least one symbol.  | 
 | 122 | +            LongerPassword!            | The password field must contain at least one number.  | 
 | 123 | +            password123                | The password field must contain at least one uppercase and one lowercase letter.  | 
 | 124 | +            PASSWORD123                | The password field must contain at least one uppercase and one lowercase letter.  | 
 | 125 | +            """)  | 
 | 126 | +    void testInvalidPasswords(String invalidPassword, String expectedError) {  | 
 | 127 | +        User user = User.randomUser().withPassword(invalidPassword);  | 
 | 128 | + | 
 | 129 | +        APIResponse response = request.post("/users/register",  | 
 | 130 | +                RequestOptions.create()  | 
 | 131 | +                        .setHeader("Content-Type", "application/json")  | 
 | 132 | +                        .setData(user));  | 
 | 133 | + | 
 | 134 | +        assertThat(response.status()).isEqualTo(422);  | 
 | 135 | +        assertThat(response.text()).contains(expectedError);  | 
 | 136 | +    }  | 
 | 137 | + | 
 | 138 | +}  | 
0 commit comments