|
| 1 | +/** |
| 2 | + * E2E tests for authentication flows: User Signup and Login. |
| 3 | + * |
| 4 | + * These tests cover the primary user journeys for account creation |
| 5 | + * and logging in to the TaskNote application. |
| 6 | + */ |
| 7 | +describe('Authentication', () => { |
| 8 | + /** |
| 9 | + * User Signup (Registration) flow |
| 10 | + */ |
| 11 | + describe('Signup', () => { |
| 12 | + beforeEach(() => { |
| 13 | + cy.visit('/register'); |
| 14 | + }); |
| 15 | + |
| 16 | + it('displays the registration form', () => { |
| 17 | + cy.contains('h2', 'Create account').should('be.visible'); |
| 18 | + cy.get('input[name="email"]').should('be.visible'); |
| 19 | + cy.get('input[name="password"]').should('be.visible'); |
| 20 | + cy.get('input[name="passwordAgain"]').should('be.visible'); |
| 21 | + cy.get('button[type="submit"]').should('be.visible').and('contain', 'Create account'); |
| 22 | + }); |
| 23 | + |
| 24 | + it('shows a validation error when the form is submitted empty', () => { |
| 25 | + cy.get('button[type="submit"]').click(); |
| 26 | + cy.get('.alert-danger').should('be.visible'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('shows a validation error when passwords do not match', () => { |
| 30 | + cy.get('input[name="email"]').type('testuser@example.com'); |
| 31 | + cy.get('input[name="password"]').type('Password1!'); |
| 32 | + cy.get('input[name="passwordAgain"]').type('DifferentPass1!'); |
| 33 | + cy.get('button[type="submit"]').click(); |
| 34 | + cy.get('.alert-danger').should('be.visible'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('navigates to the login page via the login link', () => { |
| 38 | + cy.contains('a', 'Login').first().click(); |
| 39 | + cy.url().should('include', '/login'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('navigates back to the landing page via the back-to-home link', () => { |
| 43 | + cy.contains('a', 'Back to home').click(); |
| 44 | + cy.url().should('eq', `${Cypress.config('baseUrl')}/`); |
| 45 | + }); |
| 46 | + |
| 47 | + it('submits the signup form and shows a confirmation message on success', () => { |
| 48 | + cy.intercept({ method: 'PUT', url: /\/auth\/sign-up/ }, { |
| 49 | + statusCode: 201, |
| 50 | + body: { message: 'User created' } |
| 51 | + }).as('signUp'); |
| 52 | + |
| 53 | + cy.get('input[name="email"]').type(`newuser${Date.now()}@example.com`); |
| 54 | + cy.get('input[name="password"]').type('Password1!'); |
| 55 | + cy.get('input[name="passwordAgain"]').type('Password1!'); |
| 56 | + cy.get('button[type="submit"]').click(); |
| 57 | + |
| 58 | + cy.wait('@signUp'); |
| 59 | + cy.get('.alert-success').should('be.visible'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('displays an error alert when the API returns an error on signup', () => { |
| 63 | + cy.intercept({ method: 'PUT', url: /\/auth\/sign-up/ }, { |
| 64 | + statusCode: 409, |
| 65 | + body: { message: 'Email already in use' } |
| 66 | + }).as('signUpFail'); |
| 67 | + |
| 68 | + cy.get('input[name="email"]').type(`existing${Date.now()}@example.com`); |
| 69 | + cy.get('input[name="password"]').type('Password1!'); |
| 70 | + cy.get('input[name="passwordAgain"]').type('Password1!'); |
| 71 | + cy.get('button[type="submit"]').click(); |
| 72 | + |
| 73 | + cy.wait('@signUpFail'); |
| 74 | + cy.get('.alert-danger').should('be.visible'); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + /** |
| 79 | + * User Login flow |
| 80 | + */ |
| 81 | + describe('Login', () => { |
| 82 | + beforeEach(() => { |
| 83 | + cy.visit('/login'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('displays the login form', () => { |
| 87 | + cy.contains('h2', 'Login').should('be.visible'); |
| 88 | + cy.get('input[name="email"]').should('be.visible'); |
| 89 | + cy.get('input[name="password"]').should('be.visible'); |
| 90 | + cy.get('button[type="submit"]').should('be.visible').and('contain', 'Login'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('shows a validation error when the form is submitted empty', () => { |
| 94 | + cy.get('button[type="submit"]').click(); |
| 95 | + cy.get('.alert-danger').should('be.visible'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('navigates to the register page via the create account link', () => { |
| 99 | + cy.contains('a', 'Create account').click(); |
| 100 | + cy.url().should('include', '/register'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('navigates back to the landing page via the back-to-home link', () => { |
| 104 | + cy.contains('a', 'Back to home').click(); |
| 105 | + cy.url().should('eq', `${Cypress.config('baseUrl')}/`); |
| 106 | + }); |
| 107 | + |
| 108 | + it('navigates to the reset password page via the forgot password link', () => { |
| 109 | + cy.contains('a', 'Forgot your password?').click(); |
| 110 | + cy.url().should('include', '/reset-password'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('redirects to home after a successful login', () => { |
| 114 | + cy.intercept({ method: 'POST', url: /\/auth\/sign-in/ }, { |
| 115 | + statusCode: 200, |
| 116 | + body: { token: 'fake-jwt-token', email: 'user@example.com' } |
| 117 | + }).as('signIn'); |
| 118 | + |
| 119 | + cy.get('input[name="email"]').type('user@example.com'); |
| 120 | + cy.get('input[name="password"]').type('Password1!'); |
| 121 | + cy.get('button[type="submit"]').click(); |
| 122 | + |
| 123 | + cy.wait('@signIn'); |
| 124 | + cy.url().should('include', '/home'); |
| 125 | + }); |
| 126 | + |
| 127 | + it('displays an error alert when the API returns an error on login', () => { |
| 128 | + cy.intercept({ method: 'POST', url: /\/auth\/sign-in/ }, { |
| 129 | + statusCode: 401, |
| 130 | + body: { message: 'Invalid credentials' } |
| 131 | + }).as('signInFail'); |
| 132 | + |
| 133 | + cy.get('input[name="email"]').type('user@example.com'); |
| 134 | + cy.get('input[name="password"]').type('wrongpassword'); |
| 135 | + cy.get('button[type="submit"]').click(); |
| 136 | + |
| 137 | + cy.wait('@signInFail'); |
| 138 | + cy.get('.alert-danger').should('be.visible'); |
| 139 | + }); |
| 140 | + }); |
| 141 | +}); |
0 commit comments