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/shop.komma.nl/tests/Cypress/integration/site/my_account_data.spec.js
/// <reference types="Cypress" />

import faker from 'faker';
import PagesInfo from "../../support/pagesInfo";
import {G11nAccountService} from "../../support/g11n_and_address_service";

const pagesInfo = new PagesInfo();

describe('My Account', function() {
    describe('Authentication / Authorisation', function () {
        it('It should be possible to register a new customer', function() {
            cy.visit(pagesInfo.getByName('register').locations.nl);

            const user = {
                first_name: faker.name.firstName(),
                last_name_prefix: faker.random.arrayElement(['de', 'het', 'ten', 'van', 'van de', 'van den', 'van der', 'van het']),
                last_name: faker.name.lastName(),
                phone: faker.phone.phoneNumber(),
                email: faker.internet.email(),
                password: 'Admin126#!'
            }

            const accountAddress = G11nAccountService.makeAddress();

            cy.get('[data-test="first_name"]').type(user.first_name)
            cy.get('[data-test="last_name_prefix"]').type(user.last_name_prefix)
            cy.get('[data-test="last_name"]').type(user.last_name)
            cy.get('[data-test="phone"]').type(user.phone)

            cy.get('[data-test="street"]').type(accountAddress.street)
            cy.get('[data-test="house_number"]').type(accountAddress.house_number);
            cy.get('[data-test="postal_code"]').type(accountAddress.postal_code);
            cy.get('[data-test="city"]').type(accountAddress.city);
            cy.get('[data-test="country"]').select(accountAddress.country_iso3);

            cy.get('[data-test="email"]').type(user.email)

            cy.get('[data-test="password"]').type(user.password)
            cy.get('[data-test="password-preview-toggle"]').click()
            cy.get('[data-test="password"]').should('have.attr', 'type', 'text');
            cy.get('[data-test="password-preview-toggle"]').click()
            cy.get('[data-test="password"]').should('have.attr', 'type', 'password');

            cy.get('[data-test="accept_legal"]').check();

            cy.get('[data-test="register"]').click();
            cy.get('[data-test="registered"]').should('be.visible')
        })

        it('Is possible to reset the users password', function() {
            const email = faker.internet.email();
            const password = 'Admin123!#';
            const changedPassword = 'Admin321!#';

            cy.visit('/');
            //Create a user first
            cy.getCsrfToken((csrfToken) => {
                cy.request({
                    method: 'POST',
                    headers: {'X-CSRF-TOKEN': csrfToken},
                    url: '/testapi/v1/site_users/create',
                    body: {
                        attributes: {email, password},
                    }
                }).then(response => {
                    const createdUser = response.body.data;
                    createdUser.password = password //Password is never included in responses. But we add it here for convenience
                    createdUser.changed_password = changedPassword //Password is never included in responses. But we add it here for convenience
                    //Then head to the login page and login with the created user but a wrong password
                    cy.visit(pagesInfo.getByName('siteLogin').locations.nl)
                    cy.get('input[name=email]').type(createdUser.email);
                    cy.get('input[name=password]').type(createdUser.password + 'TYPO!');
                    cy.get('[type=submit]').click();

                    //Check that we see a login error
                    cy.get('[data-test="email-error"]').should('exist');

                    //Request a password reset email
                    cy.get('[data-test="reset_password"]').click();

                    cy.request('testapi/v1/mail_intercept/enable').then((response) => {
                        cy.get('input[name="email"]').type(createdUser.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 === createdUser.email) return true;
                                return false;
                            });
                            let passwordResetMail = mailsForUser[0];

                            //Validate the mail
                            cy.log('Validating the email');
                            expect(passwordResetMail.subject).to.contain('Je wachtwoord instellen');
                            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(() => {
                                cy.get('[data-test="email"]').type(createdUser.email);
                                cy.get('[data-test="password"]').type(createdUser.changed_password);
                                cy.get('[data-test="password_confirmation"]').type(createdUser.changed_password);
                                cy.get('[data-test="reset_password"]').click();
                                cy.contains('Je wachtwoord is succesvol ingesteld.').should('exist');

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

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

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

    describe('Data', function() {
        it('Is possible to change personal details and account address', function() {
             const email = faker.internet.email();
             const password = 'Admin123!#';

            cy.visit('/');
            //Create a user first
            cy.getCsrfToken((csrfToken) => {
                cy.request({
                    method: 'POST',
                    headers: {'X-CSRF-TOKEN': csrfToken},
                    url: '/testapi/v1/site_users/create',
                    body: {
                        attributes: {email, password},
                    }
                }).then(response => {
                    const createdUser = response.body.data;
                    createdUser.password = password //Password is never included in responses. But we add it here for convenience
                    //Then head to the login page and login with the created user
                    cy.site_login(pagesInfo.getByName('myAccount').locations.nl, createdUser.email, createdUser.password).then(() => {
                        //Go to the data page

                        //Verify user stuff
                        cy.get('[data-test=sidebar_data]').click();
                        cy.get('[data-test="first_name"]').should('have.value', createdUser.first_name);
                        cy.get('[data-test="last_name_prefix"]').should('have.value', createdUser?.last_name_prefix ?? '');
                        cy.get('[data-test="last_name"]').should('have.value', createdUser.last_name);
                        cy.get('[data-test="email"]').should('have.value', createdUser.email);

                        //Change the account address
                        const accountAddress = G11nAccountService.makeAddress()

                        cy.get('[data-test="street"]').clear().type(accountAddress.street)
                        cy.get('[data-test="house_number"]').clear().type(accountAddress.house_number);
                        cy.get('[data-test="postal_code"]').clear().type(accountAddress.postal_code);
                        cy.get('[data-test="city"]').clear().type(accountAddress.city);
                        cy.get('[data-test="country"]').select(accountAddress.country_iso3);

                        cy.get('[data-test="change"]').click({force: true}); //We force clicking because the cookie bar sometimes overlaps it, and we wont be bothered by that.

                        cy.get('[data-test="street"]').should('have.value', accountAddress.street)
                        cy.get('[data-test="house_number"]').should('have.value', accountAddress.house_number);
                        cy.get('[data-test="postal_code"]').should('have.value', accountAddress.postal_code);
                        cy.get('[data-test="city"]').should('have.value', accountAddress.city);
                        cy.get('[data-test="country"]').should('have.value', accountAddress.country_iso3);

                        //Delete the created user to clean up.
                        cy.getCsrfToken((csrfToken) => {
                            cy.request({
                                method: 'DELETE',
                                headers: {'X-CSRF-TOKEN': csrfToken},
                                url: '/testapi/v1/site_users/' + createdUser.id,
                            })
                        });
                    })
                })
            });
        });

        it('Is possible to change the users password', function() {
            const email = faker.internet.email();
            const password = 'Admin123!#';
            const changedPassword = 'Admin321!#';

            cy.visit('/');
            //Create a user first
            cy.getCsrfToken((csrfToken) => {
                cy.request({
                    method: 'POST',
                    headers: {'X-CSRF-TOKEN': csrfToken},
                    url: '/testapi/v1/site_users/create',
                    body: {
                        attributes: {email, password},
                    }
                }).then(response => {
                    const createdUser = response.body.data;
                    createdUser.password = password //Password is never included in responses. But we add it here for convenience
                    createdUser.changed_password = changedPassword //Password is never included in responses. But we add it here for convenience
                    //Then head to the login page and login with the created user
                    cy.site_login(pagesInfo.getByName('myAccount').locations.nl, createdUser.email, createdUser.password).then(() => {
                        //Go to the data page and verify that the password field if of type password and is empty
                        cy.get('[data-test="password"]').should('have.value', '');
                        cy.get('[data-test="password"]').should('have.attr', 'type', 'password');

                        //Enter a new value and preview it.
                        cy.get('[data-test="password"]').type(changedPassword)
                        cy.get('[data-test="password-preview-toggle"]').click()
                        cy.get('[data-test="password"]').should('have.attr', 'type', 'text');
                        cy.get('[data-test="password-preview-toggle"]').click()
                        cy.get('[data-test="password"]').should('have.attr', 'type', 'password');

                        //Save it
                        cy.get('[data-test="change"]').click({force: true}); //We force clicking because the cookie bar sometimes overlaps it, and we wont be bothered by that.

                        //Logout and log back in with the new password
                        cy.get('[data-test="sidebar_logout"]').click();
                        cy.site_login(pagesInfo.getByName('myAccount').locations.nl, createdUser.email, createdUser.changed_password).then(() => {
                            //User has been logged in with its new password
                            //Delete the created user to clean up.
                            cy.getCsrfToken((csrfToken) => {
                                cy.request({
                                    method: 'DELETE',
                                    headers: {'X-CSRF-TOKEN': csrfToken},
                                    url: '/testapi/v1/site_users/' + createdUser.id,
                                })
                            });
                        })
                    })
                })
            });
        });
    });

    describe('Addresses', function() {
        // it('Is possible to add, change and delete an address', function() {
        //      const email = faker.internet.email();
        //      const password = 'Admin123!#';
        //
        //     cy.visit('/');
        //     //Create a user first
        //     cy.getCsrfToken((csrfToken) => {
        //         cy.request({
        //             method: 'POST',
        //             headers: {'X-CSRF-TOKEN': csrfToken},
        //             url: '/testapi/v1/site_users/create',
        //             body: {
        //                 attributes: {email, password},
        //             }
        //         }).then(response => {
        //             const createdUser = response.body.data;
        //             createdUser.password = password //Password is never included in responses. But we add it here for convenience
        //             //Then head to the login page and login with the created user
        //             cy.site_login(pagesInfo.getByName('myAccount').locations.nl, createdUser.email, createdUser.password).then(() => {
        //                 //Go to the Addresses page
        //                 cy.get('[data-test=sidebar_addresses]').click();
        //                 //Verify that the user does not have an additional address specified
        //                 cy.get('[data-test=no-other-addresses]').should('exist')
        //
        //                 //add a new address
        //                 cy.get('[data-test=add-new-address]').click({force: true}); //Needed because cookie bar covers it. We don't bother the cookie bar in our test.
        //
        //                 const address = G11nAccountService.makeAddress();
        //                 address.first_name = createdUser.first_name;
        //                 address.last_name_prefix = createdUser?.last_name_prefix ?? '';
        //                 address.last_name = createdUser.last_name;
        //
        //                 for(let fieldName in address) {
        //                     if(!address.hasOwnProperty(fieldName)) continue;
        //                     if(fieldName !== 'country_iso3') cy.get('[data-test="'+fieldName+'"]').clear();
        //                     if(address[fieldName]) cy.get('[data-test="'+fieldName+'"]').type(address[fieldName])
        //                 }
        //                 cy.get('[data-test="save"]').click({force:true});
        //
        //                 //Check that it has been added
        //                 cy.get('[data-test=no-other-addresses]').should('not.exist')
        //                 cy.get('[data-test=other-address]').should('have.length', 1)
        //
        //                 //Change the address data. And change country to
        //                 const editedAddress = Object.assign({}, address)
        //                 for(let fieldName in address) {
        //                     if(!address.hasOwnProperty(fieldName)) continue;
        //                     editedAddress[fieldName] = (fieldName !== 'country_iso3') ? editedAddress[fieldName] + ' edited' : G11nAccountService.randomCountryIso3();
        //                 }
        //
        //                 //Type the new address data and save it.
        //                 cy.get('[data-test=edit-other-address]').click();
        //                 for(let fieldName in editedAddress) {
        //                     if(!editedAddress.hasOwnProperty(fieldName)) continue;
        //                     if(fieldName !== 'country_iso3') {
        //                         cy.get('[data-test=' + fieldName + ']').clear()
        //                         if (editedAddress[fieldName]) cy.get('[data-test=' + fieldName + ']').type(editedAddress[fieldName]);
        //                     } else {
        //                         cy.get('[data-test=' + fieldName + ']').select(editedAddress[fieldName]);
        //                     }
        //                 }
        //                 cy.get('[data-test=save]').click()
        //
        //                 //Validate that it is edited
        //                 for(let fieldName in editedAddress) {
        //                     if(!editedAddress.hasOwnProperty(fieldName)) continue;
        //                     if(fieldName !== 'country_iso3') {
        //                         cy.get('[data-test=other-address]').should('contain', editedAddress[fieldName]);
        //                     } else {
        //                         cy.get('[data-test=other-address]').should('contain', G11nAccountService.nativeCountryNameByIso3(editedAddress[fieldName]));
        //                     }
        //                 }
        //                 cy.get('[data-test=success]').should('be.visible');
        //
        //                 //Now delete it and confirm that it was.
        //                 cy.get('[data-test=delete-other-address]').click()
        //                 cy.get('[data-test=other-address]').should('not.exist');
        //                 cy.get('[data-test=success]').should('be.visible');
        //
        //                 //Delete the created user to clean up.
        //                 cy.getCsrfToken((csrfToken) => {
        //                     cy.request({
        //                         method: 'DELETE',
        //                         headers: {'X-CSRF-TOKEN': csrfToken},
        //                         url: '/testapi/v1/site_users/' + createdUser.id,
        //                     })
        //                 });
        //             })
        //         })
        //     });
        // });

        it('Is possible to mark an address as invoice and / or shipping address', function() {
            const email = faker.internet.email();
            const password = 'Admin123!#';

            cy.visit('/');
            //Create a user first
            cy.getCsrfToken((csrfToken) => {
                cy.request({
                    method: 'POST',
                    headers: {'X-CSRF-TOKEN': csrfToken},
                    url: '/testapi/v1/site_users/create',
                    body: {
                        attributes: {email, password},
                    }
                }).then(response => {
                    const createdUser = response.body.data;
                    createdUser.password = password //Password is never included in responses. But we add it here for convenience
                    //Then head to the login page and login with the created user
                    cy.site_login(pagesInfo.getByName('myAccount').locations.nl, createdUser.email, createdUser.password).then(() => {
                        //Go to the Addresses page
                        cy.get('[data-test=sidebar_addresses]').click();
                        //Verify that the user does not have an additional address specified
                        cy.get('[data-test=no-other-addresses]').should('exist')

                        //Verify that the users account address is marked as both the shipping as invoice address
                        cy.get('[data-test=mark-account-as-shipping-address]').should('have.attr', 'aria-checked')
                        cy.get('[data-test=mark-account-as-invoice-address]').should('have.attr', 'aria-checked')

                        //add a new address
                        cy.get('[data-test=add-new-address]').click({force: true}); //Needed because cookie bar covers it. We don't bother the cookie bar in our test.

                        const address = G11nAccountService.makeAddress();
                        address.first_name = createdUser.first_name;
                        address.last_name_prefix = createdUser?.last_name_prefix ?? '';
                        address.last_name = createdUser.last_name;

                        for(let fieldName in address) {
                            if(!address.hasOwnProperty(fieldName)) continue;
                            if(fieldName !== 'country_iso3') cy.get('[data-test="'+fieldName+'"]').clear();
                            if(address[fieldName]) cy.get('[data-test="'+fieldName+'"]').type(address[fieldName])
                        }
                        cy.get('[data-test="save"]').click({force:true});

                        //Check that the added address is not marked as the shipping and invoice address. And check that the account address still is.
                        cy.get('[data-test=mark-other-as-shipping-address]').should('exist')
                        cy.get('[data-test=mark-other-as-shipping-address]').should('have.attr', 'aria-checked', 'false') //The aria checked attribute acts as a non boolean attribute, having a boolean false. Normally a boolean attribute having value false will be removed. But not when in aria context.
                        cy.get('[data-test=mark-other-as-invoice-address]').should('have.attr', 'aria-checked', 'false')

                        cy.get('[data-test=mark-account-as-shipping-address]').should('have.attr', 'aria-checked', 'true')
                        cy.get('[data-test=mark-account-as-invoice-address]').should('have.attr', 'aria-checked', 'true')

                        //Mark the new address as the shipping address. Check that the account address isn't marked as the shipping address anymore.
                        cy.get('[data-test=mark-other-as-shipping-address]').click();
                        cy.get('[data-test=mark-other-as-shipping-address]').should('have.attr', 'aria-checked', 'true')
                        cy.get('[data-test=mark-account-as-shipping-address]').should('have.attr', 'aria-checked', 'false')

                        //Mark the new address as the invoice address. Check that the account address isn't marked as the invoice address anymore.
                        cy.get('[data-test=mark-other-as-invoice-address]').click();
                        cy.get('[data-test=mark-other-as-invoice-address]').should('have.attr', 'aria-checked', 'true')
                        cy.get('[data-test=mark-account-as-invoice-address]').should('have.attr','aria-checked', 'false')

                        //Now delete the added address. The account address must become the shipping and invoice address again.
                        cy.get('[data-test=delete-other-address]').click();
                        cy.get('[data-test=no-other-addresses]').should('exist');
                        cy.get('[data-test=mark-account-as-shipping-address]').should('have.attr', 'aria-checked', 'true')
                        cy.get('[data-test=mark-account-as-invoice-address]').should('have.attr', 'aria-checked', 'true')

                        //Delete the created user to clean up.
                        cy.getCsrfToken((csrfToken) => {
                            cy.request({
                                method: 'DELETE',
                                headers: {'X-CSRF-TOKEN': csrfToken},
                                url: '/testapi/v1/site_users/' + createdUser.id,
                            })
                        });
                    })
                })
            });
        });
    });
});