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/kms/kms_users_spec.js
import faker from 'faker'

/// <reference types="Cypress" />
describe('KMS Users', function() {
    it('Cannot create a user without entering data', () =>{
        cy.kms_login('kms');
        cy.get('[data-test="Gebruikers"]').click();
        cy.get('[data-test="KMS gebruikers"]').click();
        cy.get('[data-test="add_button"]').click();

        cy.get('[data-test="save_button"]').click();
        cy.get('[data-test=flash_message]').should('have.attr', 'data-type', '1'); //Data type 1 means error.
    });

    it('Create a user with e-mail only and test send password mail button', () =>{
        cy.kms_login('kms');
        cy.get('[data-test="Gebruikers"]').click();
        cy.get('[data-test="KMS gebruikers"]').click();
        cy.get('[data-test="add_button"]').click();

        let email = faker.internet.email();
        cy.get('[data-test="TextField-email"]').type(email);

        cy.log('Telling laravel that it should intercept the next mails');
        cy.request('testapi/v1/mail_intercept/enable').then((response) => {
            cy.get('[data-test="send_password_set_mail_button"]').should('not.exist');
            cy.get('[data-test="save_button"]').click();
            cy.get('[data-test=flash_message]').should('have.attr', 'data-type', '2'); //Data type 2 means success.
            cy.get('[data-test="send_password_set_mail_button"]').click();
            cy.get('[data-test="mail_sent"]').should('be.visible');
            cy.get('[data-test="mail_error"]').should('not.be.visible');

            cy.get('[data-test="entity_tab_algemeen"]').click();
            cy.get('[data-test="TextField-email"]').should('have.value', email);

            cy.request('testapi/v1/mail_intercept/get').then((response) => {
                let mails = response.body;
                let passwordSetMail = mails[0];

                //Validate the mail
                cy.log('Validating the password set email');
                expect(passwordSetMail.subject).to.contain('Welkom in het Komma Management Systeem');
                expect(passwordSetMail).to.have.property('data');
                expect(passwordSetMail.data).to.have.property('setPasswordUrl');

                //Construct the reset url and visit it.
                cy.log('Heading to the password set page');
                cy.clearCookies();
                cy.visit(passwordSetMail.data.setPasswordUrl).then(() => {
                    cy.log('Setting the password');
                    let newPassword = faker.internet.password()+'_!A1';
                    cy.get('[data-test="email"]').type(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(email);
                    cy.get('input[name=password]').type(newPassword);
                    cy.get('input[type=submit]').click();

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

    it('Create a user with e-mail, password and first name and test login', () =>{
        //Login and open the KMS users section
        cy.kms_login('kms');
        cy.get('[data-test="Gebruikers"]').click();
        cy.get('[data-test="KMS gebruikers"]').click();
        cy.get('[data-test="add_button"]').click();

        //Generate credential data
        let email = faker.internet.email();
        let password = faker.internet.password()+'_!A1';
        let firstName = faker.name.firstName();

        //Create the user
        cy.get('[data-test="TextField-first_name"]').type(firstName);
        cy.get('[data-test="TextField-email"]').type(email);
        cy.get('[data-test="Password-password-1"]').type(password);
        cy.wait(150);
        cy.get('[data-test="Password-password-2"]').type(password);
        cy.wait(150);
        cy.get('[data-test="save_button"]').click({force: true});
        cy.get('[data-test=flash_message]').should('have.attr', 'data-type', '2'); //Data type 2 means success.
        cy.get('[data-test="entity_tab_algemeen"]').click();
        cy.get('[data-test="TextField-first_name"]').should('have.value', firstName);
        cy.get('[data-test="TextField-email"]').should('have.value', email);

        //Logout and test login
        cy.get('[data-test="log-out"]').click();
        cy.get('[data-test="email"]').type(email);
        cy.get('[data-test="password"]').type(password);
        cy.get('[data-test="submit"]').click();
        cy.get('[data-test="Gebruikers"]').should('exist');
    });

    it('Should be possible to search for a user', () => {
        cy.kms_login('kms');
        cy.get('[data-test="Gebruikers"]').click();
        cy.get('[data-test="KMS gebruikers"]').click();

        //Get a user to search for first
        cy.getCsrfToken((csrfToken) => {
            cy.request(
                'testapi/v1/kms_users/show/',
            ).then((response) => {
                let user = response.body.data;
                cy.get('[data-test="entity_search_input"]').type(user.email);
                cy.get('[data-test="search-result-counter"]').contains('1').should('exist');
            });
        });
    });
});