Skip to content

Commit ff430d2

Browse files
committed
Sample API tests
1 parent b373e1b commit ff430d2

File tree

6 files changed

+167
-157
lines changed

6 files changed

+167
-157
lines changed

pom.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,10 @@
9191
<scope>test</scope>
9292
</dependency>
9393
<dependency>
94-
<groupId>com.github.javafaker</groupId>
95-
<artifactId>javafaker</artifactId>
96-
<version>1.0.2</version>
94+
<groupId>net.datafaker</groupId>
95+
<artifactId>datafaker</artifactId>
96+
<version>2.4.2</version>
97+
<scope>test</scope>
9798
</dependency>
9899
</dependencies>
99100
<build>
Lines changed: 25 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,48 @@
11
package com.serenitydojo.playwright.toolshop.domain;
22

3-
import com.github.javafaker.Faker;
3+
import net.datafaker.Faker;
44

5-
import java.time.LocalDate;
6-
import java.time.format.DateTimeFormatter;
7-
import java.util.Date;
8-
9-
/**
10-
* {
11-
* "first_name": "John",
12-
* "last_name": "Doe",
13-
* "address": "Street 1",
14-
* "city": "City",
15-
* "state": "State",
16-
* "country": "Country",
17-
* "postcode": "1234AA",
18-
* "phone": "0987654321",
19-
* "dob": "1970-01-01",
20-
* "password": "S1!uper-secret",
21-
* "email": "[email protected]"
22-
* }
23-
*/
245
public record User(
25-
String first_name,
26-
String last_name,
27-
String address,
28-
String city,
29-
String state,
30-
String country,
31-
String postcode,
32-
String phone,
33-
String dob,
34-
String password,
35-
String email
36-
) {
6+
String first_name,
7+
String last_name,
8+
String address,
9+
String city,
10+
String state,
11+
String country,
12+
String postcode,
13+
String phone,
14+
String dob,
15+
String password,
16+
String email) {
3717
public static User randomUser() {
3818
Faker fake = new Faker();
39-
int year = fake.number().numberBetween(1970,2000);
40-
int month = fake.number().numberBetween(1,12);
41-
int day = fake.number().numberBetween(1,28);
42-
LocalDate date = LocalDate.of(year,month,day);
43-
String formattedDate = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
44-
4519
return new User(
4620
fake.name().firstName(),
4721
fake.name().lastName(),
4822
fake.address().streetAddress(),
4923
fake.address().city(),
5024
fake.address().state(),
5125
fake.address().country(),
52-
fake.address().zipCode(),
26+
fake.address().postcode(),
5327
fake.phoneNumber().phoneNumber(),
54-
formattedDate,
55-
"Az1234£!3",
28+
"1990-01-01",
29+
"Az123!&xyz",
5630
fake.internet().emailAddress()
5731
);
5832
}
5933

6034
public User withPassword(String password) {
6135
return new User(
62-
this.first_name,
63-
this.last_name,
64-
this.address,
65-
this.city,
66-
this.state,
67-
this.country,
68-
this.postcode,
69-
this.phone,
70-
this.dob,
36+
first_name,
37+
last_name,
38+
address,
39+
city,
40+
state,
41+
country,
42+
postcode,
43+
phone,
44+
dob,
7145
password,
72-
this.email
73-
);
46+
email);
7447
}
7548
}

src/test/java/com/serenitydojo/playwright/toolshop/login/LoginPage.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/test/java/com/serenitydojo/playwright/toolshop/login/LoginWithRegisteredUserTest.java

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
}

src/test/java/com/serenitydojo/playwright/toolshop/login/UserAPIClient.java

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)