From fc3dfdfd487b4a9e52a92d1631209c14a85be670 Mon Sep 17 00:00:00 2001 From: Sami Date: Tue, 15 Oct 2024 03:15:51 -0400 Subject: [PATCH 1/2] day 7: Authentication and Authorization --- pom.xml | 3 --- .../zacksolutions/DaySeven/AuthenticationTest.java | 13 ++++++++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index acf3b55..6019150 100644 --- a/pom.xml +++ b/pom.xml @@ -6,11 +6,8 @@ restAssured 1.0-SNAPSHOT jar - restAssured http://maven.apache.org - - 8 8 diff --git a/src/test/java/zacksolutions/DaySeven/AuthenticationTest.java b/src/test/java/zacksolutions/DaySeven/AuthenticationTest.java index cc0ea0b..9e95868 100644 --- a/src/test/java/zacksolutions/DaySeven/AuthenticationTest.java +++ b/src/test/java/zacksolutions/DaySeven/AuthenticationTest.java @@ -74,8 +74,19 @@ public class AuthenticationTest { @Test(priority = 7) void testAPIKeyAuth() { +/* THIS METHOD 1 given().queryParam("appid", "53f8437943998c07789a6693aec69607") - .when().get("https://api.openweathermap.org/data/2.5/weather?q=Chicago&appid=53f8437943998c07789a6693aec69607") + .when().get("https://api.openweathermap.org/data/2.5/weather?q=New York&appid=53f8437943998c07789a6693aec69607") .then().statusCode(200).log().all(); +*/ + //METHOD 2 + given() + .pathParam("myPath", "data/2.5/weather") + .queryParam("q", "New York") + .queryParam("appid", "53f8437943998c07789a6693aec69607") + .when() + .get("https://api.openweathermap.org/{myPath}") + .then().statusCode(200).log().all(); + } } From f34444eac49bdea3adef9edf847bb23b235fdfd6 Mon Sep 17 00:00:00 2001 From: Sami Date: Tue, 15 Oct 2024 04:33:12 -0400 Subject: [PATCH 2/2] day 8: Chaining and TestNG --- .../DayEight/CreateUserTest.java | 32 +++++++++++++++++ .../DayEight/DeleteUserTest.java | 27 ++++++++++++++ .../zacksolutions/DayEight/GetUserTest.java | 28 +++++++++++++++ .../DayEight/UpdateUserTest.java | 36 +++++++++++++++++++ 4 files changed, 123 insertions(+) create mode 100644 src/test/java/zacksolutions/DayEight/CreateUserTest.java create mode 100644 src/test/java/zacksolutions/DayEight/DeleteUserTest.java create mode 100644 src/test/java/zacksolutions/DayEight/GetUserTest.java create mode 100644 src/test/java/zacksolutions/DayEight/UpdateUserTest.java diff --git a/src/test/java/zacksolutions/DayEight/CreateUserTest.java b/src/test/java/zacksolutions/DayEight/CreateUserTest.java new file mode 100644 index 0000000..06bf45e --- /dev/null +++ b/src/test/java/zacksolutions/DayEight/CreateUserTest.java @@ -0,0 +1,32 @@ +package zacksolutions.DayEight; + +import static io.restassured.RestAssured.*; +import org.json.JSONObject; +import org.testng.ITestContext; +import org.testng.annotations.Test; + +/** + * CreateUserTest + */ +public class CreateUserTest { + + @Test + void createUser(ITestContext context) { + + String bearerToken = "2fjksdc32opcmlpqweldien324fwefwnklploqpiwuioennxmhjgasdfe"; + + JSONObject data = new JSONObject(); + data.put("name", "Zakaria"); + data.put("gender", "male"); + data.put("email", "zakaria@gmail.com"); + data.put("status", "active"); + + int id = given().header("Authorization", "Bearer " + bearerToken).contentType("Application/json") + .body(data.toString()) + .when() + .post("https://foobar.com/public/v2/users/") + .jsonPath().getInt("id"); + + context.getSuite().setAttribute("user_ID", id); + } +} diff --git a/src/test/java/zacksolutions/DayEight/DeleteUserTest.java b/src/test/java/zacksolutions/DayEight/DeleteUserTest.java new file mode 100644 index 0000000..c6cf4c5 --- /dev/null +++ b/src/test/java/zacksolutions/DayEight/DeleteUserTest.java @@ -0,0 +1,27 @@ +package zacksolutions.DayEight; + +import org.testng.ITestContext; +import org.testng.annotations.Test; +import static io.restassured.RestAssured.*; + +/** + * DeleteUserTest + */ +public class DeleteUserTest { + @Test + void delete_User(ITestContext context) { + + int id = (Integer) context.getSuite().getAttribute("user_id"); + + String bearerToken = "2fjksdc32opcmlpqweldien324fwefwnklploqpiwuioennxmhjgasdfe"; + + given() + .header("Authorization", "Bearer " + bearerToken) + .pathParam("id", id) + .when() + .delete("https://foobar.com/public/v2/users/{id}") + .then().statusCode(204) + .log().all(); + } + +} diff --git a/src/test/java/zacksolutions/DayEight/GetUserTest.java b/src/test/java/zacksolutions/DayEight/GetUserTest.java new file mode 100644 index 0000000..96c66f4 --- /dev/null +++ b/src/test/java/zacksolutions/DayEight/GetUserTest.java @@ -0,0 +1,28 @@ +package zacksolutions.DayEight; + +import static io.restassured.RestAssured.*; + +import org.testng.ITestContext; +import org.testng.annotations.Test; + +/** + * GetUserTest + */ +public class GetUserTest { + + @Test + void get_User(ITestContext context) { + + int id = (Integer) context.getSuite().getAttribute("user_id"); + String bearerToken = "2fjksdc32opcmlpqweldien324fwefwnklploqpiwuioennxmhjgasdfe"; + + given() + .header("Authorizartion", "Bearer " + bearerToken) + .pathParam("id", id) + .when() + .get("https://foobar.com.public/v2/users/") + .then() + .statusCode(200) + .log().all(); + } +} diff --git a/src/test/java/zacksolutions/DayEight/UpdateUserTest.java b/src/test/java/zacksolutions/DayEight/UpdateUserTest.java new file mode 100644 index 0000000..0fc9fe0 --- /dev/null +++ b/src/test/java/zacksolutions/DayEight/UpdateUserTest.java @@ -0,0 +1,36 @@ +package zacksolutions.DayEight; + +import org.json.JSONObject; +import org.testng.ITestContext; +import org.testng.annotations.Test; + +import static io.restassured.RestAssured.given; + +/** + * UpdateUserTest + */ +public class UpdateUserTest { + + @Test + void update_User(ITestContext context) { + int id = (Integer) context.getSuite().getAttribute("user_id"); + + String bearerToken = "2fjksdc32opcmlpqweldien324fwefwnklploqpiwuioennxmhjgasdfe"; + + JSONObject data = new JSONObject(); + data.put("name", "Zakaria"); + data.put("gender", "male"); + data.put("email", "zakaria@gmail.com"); + data.put("status", "Superactive"); + + given() + .header("Authorization", "Bearer " + bearerToken) + .contentType("Application/json") + .body(data.toString()) + .pathParam("id", id) + .when() + .put("https://foobar.com/public/v2/users/{id}") + .then().statusCode(200) + .log().all(); + } +}