|
| 1 | +import { seededUsers } from '../../support/seeded-users.generated'; |
| 2 | + |
| 3 | +describe('Create Tenant: redirect to invites', () => { |
| 4 | + it('should redirect to invites page when user has pending invites', () => { |
| 5 | + const ts = Date.now(); |
| 6 | + const tenantName = `InviteRedirectTenant${ts}`; |
| 7 | + const tenantSlug = `invite-redirect-tenant-${ts}`; |
| 8 | + |
| 9 | + // Step 1: Login as owner and create a tenant |
| 10 | + cy.visit('/auth/login'); |
| 11 | + cy.get('input#email').type(seededUsers.owner.email); |
| 12 | + cy.get('input#password').type(seededUsers.owner.password); |
| 13 | + cy.get('form') |
| 14 | + .filter(':visible') |
| 15 | + .first() |
| 16 | + .within(() => { |
| 17 | + cy.contains('button', /^Sign In$/) |
| 18 | + .should('be.enabled') |
| 19 | + .click(); |
| 20 | + }); |
| 21 | + cy.location('pathname', { timeout: 30000 }).should( |
| 22 | + 'match', |
| 23 | + /\/tenants\/.+/, |
| 24 | + ); |
| 25 | + |
| 26 | + // Create a new tenant for the invite |
| 27 | + cy.request({ |
| 28 | + method: 'POST', |
| 29 | + url: '/api/v1/tenants', |
| 30 | + body: { |
| 31 | + name: tenantName, |
| 32 | + slug: tenantSlug, |
| 33 | + environment: 'development', |
| 34 | + }, |
| 35 | + }) |
| 36 | + .its('status') |
| 37 | + .should('eq', 200); |
| 38 | + |
| 39 | + // Refresh to get the new tenant |
| 40 | + cy.visit('/'); |
| 41 | + cy.location('pathname', { timeout: 30000 }).should( |
| 42 | + 'match', |
| 43 | + /\/tenants\/.+/, |
| 44 | + ); |
| 45 | + |
| 46 | + // Switch to the new tenant |
| 47 | + cy.get('button[aria-label="Select a tenant"]') |
| 48 | + .filter(':visible') |
| 49 | + .first() |
| 50 | + .click({ force: true }); |
| 51 | + cy.get('[data-cy="tenant-switcher-list"]').should('be.visible'); |
| 52 | + cy.get(`[data-cy="tenant-switcher-item-${tenantSlug}"]`) |
| 53 | + .should('exist') |
| 54 | + .scrollIntoView() |
| 55 | + .click({ force: true }); |
| 56 | + |
| 57 | + // Get tenant ID from URL |
| 58 | + cy.location('pathname', { timeout: 30000 }) |
| 59 | + .should('match', /\/tenants\/([^/]+)/) |
| 60 | + .then((pathname) => { |
| 61 | + const match = pathname.match(/\/tenants\/([^/]+)/); |
| 62 | + const tenantId = match![1]; |
| 63 | + |
| 64 | + // Step 2: Create an invite for the member user |
| 65 | + cy.request({ |
| 66 | + method: 'POST', |
| 67 | + url: `/api/v1/tenants/${tenantId}/invites`, |
| 68 | + body: { |
| 69 | + email: seededUsers.member.email, |
| 70 | + role: 'MEMBER', |
| 71 | + }, |
| 72 | + }).then((response) => { |
| 73 | + expect(response.status).to.eq(201); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + // Step 3: Logout |
| 78 | + cy.get('button[aria-label="User Menu"]') |
| 79 | + .filter(':visible') |
| 80 | + .should('be.visible') |
| 81 | + .first() |
| 82 | + .click(); |
| 83 | + cy.contains('[role="menuitem"]', 'Log out').filter(':visible').click(); |
| 84 | + cy.location('pathname').should('include', '/auth/login'); |
| 85 | + |
| 86 | + // Step 4: Login as member (who has pending invite) |
| 87 | + cy.get('input#email').type(seededUsers.member.email); |
| 88 | + cy.get('input#password').type(seededUsers.member.password); |
| 89 | + cy.get('form') |
| 90 | + .filter(':visible') |
| 91 | + .first() |
| 92 | + .within(() => { |
| 93 | + cy.contains('button', /^Sign In$/) |
| 94 | + .should('be.enabled') |
| 95 | + .click(); |
| 96 | + }); |
| 97 | + |
| 98 | + // Wait for the navigation after sign in to complete |
| 99 | + cy.location('pathname', { timeout: 30000 }).should('not.eq', '/auth/login'); |
| 100 | + |
| 101 | + // Step 5: Try to navigate to create-tenant page |
| 102 | + // The user should be redirected to invites page |
| 103 | + cy.visit('/onboarding/create-tenant', { failOnStatusCode: false }); |
| 104 | + |
| 105 | + // Step 6: Verify redirect to invites page |
| 106 | + cy.location('pathname', { timeout: 10000 }).should( |
| 107 | + 'eq', |
| 108 | + '/onboarding/invites', |
| 109 | + ); |
| 110 | + |
| 111 | + // Verify the invite is displayed |
| 112 | + cy.contains(`invited to join the ${tenantName} tenant`).should( |
| 113 | + 'be.visible', |
| 114 | + ); |
| 115 | + |
| 116 | + // Step 7: Accept the invite to clean up (prevent affecting other tests) |
| 117 | + cy.intercept('POST', '/api/v1/users/invites/accept').as('acceptInvite'); |
| 118 | + cy.contains(`invited to join the ${tenantName} tenant`) |
| 119 | + .parent() |
| 120 | + .contains('button', 'Accept') |
| 121 | + .should('be.visible') |
| 122 | + .click(); |
| 123 | + |
| 124 | + cy.wait('@acceptInvite').its('response.statusCode').should('eq', 200); |
| 125 | + |
| 126 | + // Verify redirect to tenant page |
| 127 | + cy.location('pathname', { timeout: 10000 }).should( |
| 128 | + 'match', |
| 129 | + /\/tenants\/[^/]+/, |
| 130 | + ); |
| 131 | + }); |
| 132 | +}); |
0 commit comments