package zacksolutions.DaySeven; import static org.hamcrest.Matchers.equalTo; import org.testng.annotations.Test; import static io.restassured.RestAssured.*; /** * Authentication */ public class AuthenticationTest { @Test(priority = 1) void testBasicAuth() { System.out.println("Starting the authenticantion using Basic auth"); given().auth() .basic("postman", "password") .when().get("https://postman-echo.com/basic-auth") .then().statusCode(200).body("authenticated", equalTo(true)).log().all(); } @Test(priority = 2) void testDigestAuth() { System.out.println("Starting the authenticantion using Digest auth"); given().auth() .digest("postman", "password") .when().get("https://postman-echo.com/basic-auth") .then().statusCode(200).body("authenticated", equalTo(true)).log().all(); } @Test(priority = 3) void testPreemptiveAuth() { System.out.println("Starting the authenticantion using Preemptive auth"); given().auth() .preemptive().basic("postman", "password") .when().get("https://postman-echo.com/basic-auth") .then().statusCode(200).body("authenticated", equalTo(true)).log().all(); } @Test(priority = 4) void testBearerTokenAuth() { System.out.println("Starting the authenticantion using Bearer Token auth"); String bearerToken = "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"; given().headers("Authorization", "Bearer " + bearerToken) .when().get("https://api.github.com/user/repos") .then().statusCode(200).log().all(); } // @Test(priority = 5) void testOAuth1Auth() { System.out.println("Starting the authenticantion using OAuth1 auth"); given() .auth().oauth("consumerKey", "consumerSecret", "accessToken", "tokenSecret") .when() .get("https://api.github.com/user/repos") .then().statusCode(200).log().all(); } @Test(priority = 6) void testOAuth2Auth() { System.out.println("Starting the authenticantion using OAuth2 auth"); given() .auth().oauth2("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") .when() .get("https://api.github.com/user/repos") .then().statusCode(200).log().all(); } @Test(priority = 7) void testAPIKeyAuth() { /* THIS METHOD 1 given().queryParam("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(); } }