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/checkout.spec.js
/// <reference types="Cypress" />

import PagesInfo from "../../support/pagesInfo";
const pagesInfo = new PagesInfo();
import faker from 'faker';
import {G11nAccountService} from "../../support/g11n_and_address_service";

describe('Checkout', function() {
    it('Should be possible to successfully pay as guest', function() {
        cy.clearCookies();
        cy.request('GET', '/testapi/v1/cart/addToCart', {
            items: [
                {'productable_type' : 'product', 'id' : 2, 'quantity': 2}, //Hammer
            ]
        }).then(function(response) {
            cy.visit('/'); //Needed because previous test already is on the same page, and therefore the page still may contain state.
            cy.visit(pagesInfo.getByName('customer_details').locations.nl)

            const contactDetails = {
                firstName: faker.name.firstName(),
                lastNamePrefix: faker.random.arrayElement(['van de', 'de', 'aan', 'aan de', 'ten', 'over den']),
                lastName: faker.name.lastName(),
                email: faker.internet.email(),
                phone: faker.phone.phoneNumberFormat()
            }

            const invoiceDetails = {
                postalCode: faker.address.zipCode(),
                city: faker.address.city(),
                streetName: faker.address.streetName(),
                houseNumber: faker.address.streetAddress(),
            }

            cy.get('[data-test="first_name"]').type(contactDetails.firstName)
            cy.get('[data-test="last_name_prefix"]').type(contactDetails.lastNamePrefix)
            cy.get('[data-test="last_name"]').type(contactDetails.lastName)
            cy.get('[data-test="email"]').type(contactDetails.email)
            cy.get('[data-test="phone"]').type(contactDetails.phone)

            cy.get('[data-test="invoice_street"]').type(invoiceDetails.streetName)
            cy.get('[data-test="invoice_house_number"]').type(invoiceDetails.houseNumber)
            cy.get('[data-test="invoice_postal_code"]').type(invoiceDetails.postalCode)
            cy.get('[data-test="invoice_city"]').type(invoiceDetails.city)

            cy.get('[data-test="continue"]').click({force: true})

            cy.get('[data-test="terms-and-conditions"]').should('not.be.checked');
            cy.get('[data-test="terms-and-conditions"]').check();
            cy.get('[data-test="confirm-and-pay"]').click({force: true});

            cy.get('[data-test="komma-psp"]').should('exist')

            cy.get('[data-test="payment_status"]').select('paid');
            cy.get('[data-test="redir"]').click();

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

    it('Should be possible to change contact details when checking out', function() {
        const email = faker.internet.email();
        const password = 'Admin123!#';

        cy.visit('/');

        //Create a user
        cy.getCsrfToken((csrfToken) => {
            cy.request({
                method: 'POST',
                headers: {'X-CSRF-TOKEN': csrfToken},
                url: '/testapi/v1/site_users/create',
                body: {
                    attributes: {email, password, is_guest: false},
                }
            }).then(response => {
                //Give it the password, just for convenience further on
                const createdUser = response.body.data;
                createdUser.password = password;

                //Add a product to the cart
                cy.clearCookies();
                cy.request('GET', '/testapi/v1/cart/addToCart', {
                    items: [
                        {'productable_type' : 'product', 'id' : 1, 'quantity': 2},
                    ]
                }).then(() => {
                    //Login with the created user
                    cy.site_login(pagesInfo.getByName('myAccount').locations.nl, createdUser.email, createdUser.password).then(() => {
                        const newUserDetails = G11nAccountService.makeUser();

                        //Head to the cart and start the checkout
                        cy.get('[data-test=cart]').click();
                        cy.get('[data-test=checkout]').click();

                        //Verify that the contact details are visible
                        cy.get('[data-test=contact-details]').should('contain', createdUser.email);
                        cy.get('[data-test=contact-details]').should('contain', createdUser.telephone);

                        //Change the contact details
                        //First verify we see the current...
                        cy.get('[data-test=change-contact-details]').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 ? 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)
                        cy.get('[data-test=telephone]').should('have.value', createdUser.telephone)
                        //...Then change
                        cy.get('[data-test=first_name]').clear().type(newUserDetails.first_name);
                        cy.get('[data-test=last_name_prefix]').clear();
                        if(newUserDetails.last_name_prefix !== '') cy.get('[data-test=last_name_prefix]').type(newUserDetails.last_name_prefix);
                        cy.get('[data-test=last_name]').clear().type(newUserDetails.last_name);
                        cy.get('[data-test=email]').clear().type(newUserDetails.email);
                        cy.get('[data-test=telephone]').clear().type(newUserDetails.telephone);
                        cy.get('[data-test=save]').click();

                        //Verify that it changed
                        cy.get('[data-test=contact-details]').should('contain', newUserDetails.email);
                        cy.get('[data-test=contact-details]').should('contain', newUserDetails.telephone);

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

    it('Should show an exception when something goes wrong in the payment process', function() {
        cy.clearCookies();
        cy.request('GET', '/testapi/v1/cart/addToCart', {
            items: [
                {'productable_type' : 'product', 'id' : 2, 'quantity': 2}, //Hammer
            ]
        }).then(function(response) {
            cy.visit('/'); //Needed because previous test already is on the same page, and therefore the page still may contain state.
            cy.visit(pagesInfo.getByName('customer_details').locations.nl)

            const contactDetails = {
                firstName: faker.name.firstName(),
                lastNamePrefix: faker.random.arrayElement(['van de', 'de', 'aan', 'aan de', 'ten', 'over den']),
                lastName: faker.name.lastName(),
                email: faker.internet.email(),
                phone: faker.phone.phoneNumberFormat()
            }

            const invoiceDetails = {
                postalCode: faker.address.zipCode(),
                city: faker.address.city(),
                streetName: faker.address.streetName(),
                houseNumber: faker.address.streetAddress(),
            }

            cy.get('[data-test="first_name"]').type(contactDetails.firstName)
            cy.get('[data-test="last_name_prefix"]').type(contactDetails.lastNamePrefix)
            cy.get('[data-test="last_name"]').type(contactDetails.lastName)
            cy.get('[data-test="email"]').type(contactDetails.email)
            cy.get('[data-test="phone"]').type(contactDetails.phone)

            cy.get('[data-test="invoice_street"]').type(invoiceDetails.streetName)
            cy.get('[data-test="invoice_house_number"]').type(invoiceDetails.houseNumber)
            cy.get('[data-test="invoice_postal_code"]').type(invoiceDetails.postalCode)
            cy.get('[data-test="invoice_city"]').type(invoiceDetails.city)

            cy.get('[data-test="continue"]').click({force: true})

            cy.get('[data-test="terms-and-conditions"]').should('not.be.checked');
            cy.get('[data-test="terms-and-conditions"]').check();
            cy.get('[data-test="confirm-and-pay"]').click({force: true});

            cy.get('[data-test="komma-psp"]').should('exist')

            cy.get('[data-test="payment_status"]').select('payment_unknown');
            cy.get('[data-test="redir"]').click();

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