Skip to content

Commit

Permalink
Check for calls on /user (and fail!)
Browse files Browse the repository at this point in the history
  • Loading branch information
dokterbob committed Nov 25, 2024
1 parent 4478bcf commit 489618d
Showing 1 changed file with 42 additions and 14 deletions.
56 changes: 42 additions & 14 deletions cypress/e2e/password_auth/spec.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,22 @@ describe('Password Auth', () => {
runTestServer();
});

beforeEach(() => {
cy.intercept('GET', '/user').as('user');
});

describe('when unauthenticated', () => {
describe('visiting /', () => {
beforeEach(() => {
cy.visit('/');
});

it('should attempt to and not not have permission to access /user', () => {
cy.wait('@user').then((interception) => {
expect(interception.response.statusCode).to.equal(401);
});
});

it('should redirect to login dialog', () => {
cy.location('pathname').should('eq', '/login');
cy.get("input[name='email']").should('exist');
Expand Down Expand Up @@ -42,30 +52,48 @@ describe('Password Auth', () => {
cy.get("button[type='submit']").click();
});

const loggedIn = () => {
cy.wait('@login').then((interception) => {
// Response contains `Authorization` cookie, starting with Bearer
expect(interception.response.headers).to.have.property(
'set-cookie'
);
const cookie = interception.response.headers['set-cookie'][0];
expect(cookie).to.contain('access_token');
const shouldBeLoggedIn = () => {
it('should have an access_token cookie in /login response', () => {
cy.wait('@login').then((interception) => {
expect(interception.response.statusCode).to.equal(200);

// Response contains `Authorization` cookie, starting with Bearer
expect(interception.response.headers).to.have.property(
'set-cookie'
);
const cookie = interception.response.headers['set-cookie'][0];
expect(cookie).to.contain('access_token');
});
});

cy.location('pathname').should('not.contain', '/login');
cy.get("input[name='email']").should('not.exist');
cy.get("input[name='password']").should('not.exist');
it('should request and have access to /user', () => {
cy.wait('@user').then((interception) => {
expect(interception.response.statusCode).to.equal(200);
});
});

cy.get('.step').eq(0).should('contain', 'Hello admin');
it('should not be on /login', () => {
cy.location('pathname').should('not.contain', '/login');
});

it('should not contain a login form', () => {
cy.get("input[name='email']").should('not.exist');
cy.get("input[name='password']").should('not.exist');
});

it('should show "Hello admin"', () => {
cy.get('.step').eq(0).should('contain', 'Hello admin');
});
};

it('should be logged in', loggedIn);
shouldBeLoggedIn();

describe('after reloading', () => {
beforeEach(() => {
cy.reload();
});
it('should still be logged in', loggedIn);

shouldBeLoggedIn();
});
});
});
Expand Down

0 comments on commit 489618d

Please sign in to comment.