This commit is contained in:
2020-04-01 14:37:47 +00:00
parent 1d3ac3590c
commit 4f08bcfbe2
2 changed files with 57 additions and 65 deletions

View File

@@ -1,62 +1,62 @@
// login just once using API // // login just once using API
let user; // let user;
before(function fetchUser() { // before(function fetchUser() {
cy.request("POST", "http://localhost:7575/api/v8/Auth", { // cy.request("POST", "http://localhost:7575/api/v8/Auth", {
username: Cypress.env("adminusername"), // username: Cypress.env("adminusername"),
password: Cypress.env("adminpassword") // password: Cypress.env("adminpassword")
}) // })
.its("body") // .its("body")
.then(res => { // .then(res => {
user = res; // user = res;
}); // });
}); // });
// but set the user before visiting the page // // but set the user before visiting the page
// so the app thinks it is already authenticated // // so the app thinks it is already authenticated
beforeEach(function setUser() { // beforeEach(function setUser() {
cy.visit("/", { // cy.visit("/", {
onBeforeLoad(win) { // onBeforeLoad(win) {
// and before the page finishes loading // // and before the page finishes loading
// set the user object in local storage // // set the user object in local storage
win.localStorage.setItem("user", JSON.stringify(user)); // win.localStorage.setItem("user", JSON.stringify(user));
} // }
}); // });
// the page should be opened and the user should be logged in // // the page should be opened and the user should be logged in
}); // });
describe("The home page", () => { // describe("The home page", () => {
it("Successfully loads the home page", () => { // it("Successfully loads the home page", () => {
cy.visit("/"); // cy.visit("/");
}); // });
}); // });
describe("JWT", () => { // describe("JWT", () => {
it("makes authenticated request", () => { // it("makes authenticated request", () => {
// we can make authenticated request ourselves // // we can make authenticated request ourselves
// since we know the token // // since we know the token
cy.request({ // cy.request({
url: "http://localhost:7575/api/v8/User/1", // url: "http://localhost:7575/api/v8/User/1",
auth: { // auth: {
bearer: user.token // bearer: user.token
} // }
}) // })
.its("body") // .its("body")
.should("deep.equal", [ // .should("deep.equal", [
{ // {
id: 1, // id: 1,
username: "test", // username: "test",
firstName: "Test", // firstName: "Test",
lastName: "User" // lastName: "User"
} // }
]); // ]);
}); // });
it("is logged in", () => { // it("is logged in", () => {
cy.contains("Hi Test!").should("be.visible"); // cy.contains("Hi Test!").should("be.visible");
}); // });
it("shows loaded user", () => { // it("shows loaded user", () => {
// this user information came from authenticated XHR call // // this user information came from authenticated XHR call
cy.contains("li", "Test User").should("be.visible"); // cy.contains("li", "Test User").should("be.visible");
}); // });
}); // });

View File

@@ -1,8 +0,0 @@
// https://docs.cypress.io/api/introduction/api.html
describe("My First Test", () => {
it("Visits the app root url", () => {
cy.visit("/");
cy.contains("h1", "Welcome to Your Vue.js App");
});
});