Skip to content

Commit

Permalink
Bài 7 - Authentication - PUT method
Browse files Browse the repository at this point in the history
  • Loading branch information
anhtester committed Dec 21, 2023
1 parent dd47e37 commit 54a62f6
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.anhtester.Bai3_SendRequest_GET;
package com.anhtester.Bai3_SendRequest_GETmethod;

import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.anhtester.Bai3_SendRequest_GET;
package com.anhtester.Bai3_SendRequest_GETmethod;

import io.restassured.http.ContentType;
import io.restassured.response.Response;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.anhtester.Bai3_SendRequest_GET;
package com.anhtester.Bai3_SendRequest_GETmethod;

import io.restassured.http.ContentType;
import org.testng.annotations.Test;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.anhtester.Bai5_PhuongThucPOST;
package com.anhtester.Bai5_POSTmethod;

import io.restassured.http.ContentType;
import io.restassured.path.json.JsonPath;
Expand All @@ -8,8 +8,6 @@
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.File;

import static io.restassured.RestAssured.given;

public class DemoPostMethod {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.anhtester.Bai7_Authentication_PUTmethod;

import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.testng.annotations.Test;

import static io.restassured.RestAssured.given;

public class BasicAuth {
@Test
public void getData() {
RequestSpecification httpRequest = given();
Response response = httpRequest.get("https://postman-echo.com/basic-auth");

System.out.println("Data from the GET API: ");
System.out.println(response.getStatusCode());
System.out.println(response.getBody().asString());

response.then().statusCode(200);
}

@Test
public void testBasicAuth() {
RequestSpecification httpRequest = given().auth().basic("postman", "password");

Response response = httpRequest.get("https://postman-echo.com/basic-auth");

System.out.println("Data from the GET API: ");
System.out.println(response.getStatusCode());
System.out.println(response.getBody().asString());

response.then().statusCode(200);
}

@Test
public void testPreemptiveBasicAuth() {
RequestSpecification httpRequest = given().auth().preemptive().basic("postman", "password");

Response response = httpRequest.get("https://postman-echo.com/basic-auth");

System.out.println("Data from the GET API: ");
System.out.println(response.getStatusCode());
System.out.println(response.getBody().asString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.anhtester.Bai7_Authentication_PUTmethod;

import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.testng.annotations.Test;

import static io.restassured.RestAssured.given;

public class DigestAuth {
@Test
public void testDigestAuth() {
RequestSpecification httpRequest = given().auth().digest("postman", "password");

Response response = httpRequest.get("https://postman-echo.com/basic-auth");

System.out.println("Data from the GET API: ");
System.out.println(response.getStatusCode());
System.out.println(response.getBody().asString());

response.then().statusCode(200);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.anhtester.Bai7_Authentication_PUTmethod;

import org.testng.annotations.Test;

import static io.restassured.RestAssured.given;

public class FormAuth {

@Test
public void testFormAuth() {
given()
.auth()
.form("value1", "value2")
.get("your end point URL");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.anhtester.Bai7_Authentication_PUTmethod;

import org.testng.annotations.Test;

import static io.restassured.RestAssured.given;

public class Oauth {
@Test
public void testOAuth1() {
given()
.auth()
.oauth("consumerKey", "consumerSecret", "accessToken", "tokenSecret")
.get("your end point URL");

}

@Test
public void testOAuth2() {
given()
.auth()
.oauth2("Access token")
.get("your end point URL");

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.anhtester.Bai7_Authentication_PUTmethod;

import com.anhtester.model.LoginPOJO;
import com.anhtester.model.RegisterUserPOJO;
import com.google.gson.Gson;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static io.restassured.RestAssured.given;

public class PUTmethod_TOKEN_COOKIE {

//Khai báo biến toàn tục TOKEN để lưu trữ từ hàm Login
String TOKEN = "";

@BeforeMethod
public void loginUser() {

//Khởi tạo giá trị cho các fields thông qua hàm xây dựng
LoginPOJO loginPOJO = new LoginPOJO("anhtester", "Demo@123");

//Dùng thư viện Gson để chuyển class POJO về dạng JSON
Gson gson = new Gson();

RequestSpecification request = given();
request.baseUri("https://api.anhtester.com/api")
.accept("application/json")
.contentType("application/json")
.body(gson.toJson(loginPOJO));

Response response = request.when().post("/login");
response.prettyPrint();

response.then().statusCode(200);

//Lưu giá trị token vào biến TOKEN nhé
TOKEN = response.getBody().path("token");
System.out.println(TOKEN);
}

@Test
public void testEditUser_NoAuth() {

//Chuẩn bị data
RegisterUserPOJO registerUserPOJO = new RegisterUserPOJO();
registerUserPOJO.setUsername("myduyen5");
registerUserPOJO.setPassword("Demo@123");
registerUserPOJO.setFirstName("Lê Thị");
registerUserPOJO.setLastName("Mỹ Duyên");
registerUserPOJO.setEmail("[email protected]");
registerUserPOJO.setPhone("0123456789");
registerUserPOJO.setUserStatus(1);

Gson gson = new Gson();

RequestSpecification request = given();
request.baseUri("https://api.anhtester.com/api")
.accept("application/json")
.contentType("application/json")
.header("Authorization", "Bearer " + TOKEN)
.body(gson.toJson(registerUserPOJO));

Response response = request.when().put("/user/7");

System.out.println(response.getStatusCode());
response.prettyPrint();

response.then().statusCode(200);

String message = response.getBody().path("message");
Assert.assertEquals(message, "Success", "The message not match.");
}

}

0 comments on commit 54a62f6

Please sign in to comment.