HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/ste.komma.pro/tests/Cypress/integration/site/login_and_reset_spec.js
/// <reference types="Cypress" />

import faker from 'faker'
describe('Site Login and password reset', function() {
    it('Shows email and password required errors', function() {
        cy.clearCookies();
        cy.visit('/login');
        cy.get('[data-test="log_in"]').click();
        cy.contains('E-mailadres is verplicht.').should('exist');
        cy.contains('Wachtwoord is verplicht.').should('exist');
    });

    it('Shows wrong account error', function() {
        cy.clearCookies();
        cy.visit('/login');
        cy.get('input[name=email]').type('doesnotexist@example.org');
        cy.get('input[name=password]').type('heyhowareyou');
        cy.get('[data-test="log_in"]').click();
        cy.contains('Het opgeven e-mailadres en/of wachtwoord komen niet overeen, Probeer nogmaals?');
    });


    it('Can be logged in to', () => {
        cy.clearCookies();
        cy.visit('/login');

        cy.getCsrfToken((csrfToken) => {
            cy.request({
                method: 'GET',
                url: 'testapi/v1/site_users/create',
                headers: {'X-CSRF-TOKEN': csrfToken},
                body: {'attributes': {'password': 'SiteTest123'}}
            }).then(function (response) {
                let user = response.body.data;

                cy.get('input[name=email]').type(user.email);
                cy.get('input[name=password]').type('SiteTest123');
                cy.get('[data-test="log_in"]').click();
                cy.get('[data-test="site_logout"]').should('be.visible');

                cy.getCsrfToken((csrfToken) => {
                    cy.request({
                        method: 'DELETE',
                        url: 'testapi/v1/site_users/' + user.id,
                        headers: {'X-CSRF-TOKEN': csrfToken},
                        body: {}
                    });
                });
            });
        });
    });


    it('Should be able to reset the password', () => {
        cy.clearCookies();
        cy.visit('/login');

        cy.getCsrfToken((csrfToken) => {
            cy.request({
                method: 'GET',
                url: 'testapi/v1/site_users/create',
                headers: {'X-CSRF-TOKEN': csrfToken},
                body: {'attributes': {'password': 'SiteTest123' }}
            }).then(function(response) {
                let user = response.body.data;

                cy.log('Telling laravel that it should intercept the next mails');
                cy.request('testapi/v1/mail_intercept/enable').then((response) => {
                    cy.log('Requesting a password reset');
                    cy.get('[data-test="reset_password"]').click();
                    cy.get('input[name="email"]').type(user.email);
                    cy.get('[data-test="send_email"]').click();
                    cy.contains('We hebben een e-mail verstuurd').should('exist');

                    cy.log('Getting the latest sent mails from laravel');
                    cy.request('testapi/v1/mail_intercept/get').then((response) => {
                        let mails = response.body;

                        //Get the mails for the user only
                        let mailsForUser = mails.filter(function(mail) {
                            for (let mailAddress in mail.to) if(mailAddress === user.email) return true;
                            return false;
                        });
                        let passwordResetMail = mailsForUser[0];

                        //Validate the mail
                        cy.log('Validating the email');
                        expect(passwordResetMail.subject).to.contain('Wachtwoord resetten');
                        expect(passwordResetMail).to.have.property('data');
                        expect(passwordResetMail.data).to.have.property('resetPasswordUrl');

                        //Construct the reset url and visit it.
                        cy.log('Heading to the password reset page');
                        cy.visit(passwordResetMail.data.resetPasswordUrl).then(() => {
                            let newPassword = faker.internet.password();
                            cy.get('[data-test="email"]').type(user.email);
                            cy.get('[data-test="password"]').type(newPassword);
                            cy.get('[data-test="password_confirmation"]').type(newPassword);
                            cy.get('[data-test="reset_password"]').click();
                            cy.contains('Your password has been changed. You can log in with your new password.').should('exist');

                            cy.log('Trying to login!');
                            cy.get('input[name=email]').type(user.email);
                            cy.get('input[name=password]').type(newPassword);
                            cy.get('[data-test="log_in"]').click();

                            cy.get('[data-test="site_logout"]').should('exist');
                        });

                        cy.getCsrfToken((csrfToken) => {
                            cy.request({
                                method: 'DELETE',
                                url: 'testapi/v1/site_users/' + user.id,
                                headers: {'X-CSRF-TOKEN': csrfToken},
                                body: {}
                            });
                        });
                    });
                });
            });
        });
    })
});