<commit_before>﻿import HomePageObjects = require("../PageObjects/Organization/UsersPage.po");
import CreatePage = require("../PageObjects/Organization/CreateUserPage.po");
import CSSLocator = require("../object-wrappers/CSSLocatorHelper");

class CreateUserHelper {
    private cssHelper = new CSSLocator();
    private pageCreateObject = new CreatePage();
    private pageObject = new HomePageObjects();

    private assertCheckboxState(email: string, checkbox: protractor.ElementFinder, expectedState: boolean) {
        return this.openEditUser(email)
            .then(() => {
                var expectedValue = expectedState ? "true" : null;
                return expect(checkbox.getAttribute("checked")).toEqual(expectedValue);
            });
    }

    public checkApiRoleStatusOnUser(email: string, apiStatus: boolean) {
        return this.assertCheckboxState(email, this.pageObject.hasAPiCheckBox, apiStatus);
    }

    public checkRightsHolderAccessRoleStatusOnUser(email: string, hasAccess: boolean) {
        return this.assertCheckboxState(email, this.pageObject.hasRightsHolderAccessCheckBox, hasAccess);
    }

    public checkStakeHolderAccessRoleStatusOnUser(email: string, hasAccess: boolean) {
        return this.assertCheckboxState(email, this.pageObject.hasStakeHolderAccessCheckBox, hasAccess);
    }

    private setUserCheckboxState(email: string, checkbox: protractor.ElementFinder, toState: boolean) {
        return this.openEditUser(email)
            .then(() => {
                return checkbox.isSelected()
                    .then(selected => {
                        if (selected !== toState) {
                            return checkbox.click()
                                .then(() => {
                                    return this.pageCreateObject.editUserButton.click();
                                });
                        } else {
                            return this.pageCreateObject.cancelEditUserButton.click();
                        }
                    });
            })
            .then(() => browser.waitForAngular());
    }

    public updateApiOnUser(email: string, apiAccess: boolean) {
        return this.setUserCheckboxState(email, this.pageObject.hasAPiCheckBox, apiAccess);
    }

    public updateRightsHolderAccessOnUser(email: string, hasAccess: boolean) {
        return this.setUserCheckboxState(email, this.pageObject.hasRightsHolderAccessCheckBox, hasAccess);
    }

    public updateStakeHolderAccessOnUser(email: string, hasAccess: boolean) {
        return this.setUserCheckboxState(email, this.pageObject.hasStakeHolderAccessCheckBox, hasAccess);
    }

    private getUserRow(email: string) {
        const emailColumnElementType = "userEmailObject";

        const rows = this.pageObject.mainGridAllTableRows.filter((row, index) => {
            console.log("Searching for email column");
            var column = row.element(this.cssHelper.byDataElementType(emailColumnElementType));
            return column.isPresent()
                .then(present => {
                    if (present) {
                        console.log("Found email column - checking if row is the right one");
                        return column.getText()
                            .then(text => {
                                return text === email;
                            });
                    }
                    return false;
                });
        });

        return rows.first();
    }

    private openEditUser(email: string) {
        const row = this.getUserRow(email);
        expect(row).not.toBe(null);
        return row.element(by.linkText("Redigér")).click();<commit_msg>stabilized flaky tests using the users overview<commit_after>﻿import HomePageObjects = require("../PageObjects/Organization/UsersPage.po");
import CreatePage = require("../PageObjects/Organization/CreateUserPage.po");
import CSSLocator = require("../object-wrappers/CSSLocatorHelper");

class CreateUserHelper {
    private cssHelper = new CSSLocator();
    private pageCreateObject = new CreatePage();
    private pageObject = new HomePageObjects();

    private assertCheckboxState(email: string, checkbox: protractor.ElementFinder, expectedState: boolean) {
        return this.openEditUser(email)
            .then(() => {
                var expectedValue = expectedState ? "true" : null;
                return expect(checkbox.getAttribute("checked")).toEqual(expectedValue);
            });
    }

    public checkApiRoleStatusOnUser(email: string, apiStatus: boolean) {
        return this.assertCheckboxState(email, this.pageObject.hasAPiCheckBox, apiStatus);
    }

    public checkRightsHolderAccessRoleStatusOnUser(email: string, hasAccess: boolean) {
        return this.assertCheckboxState(email, this.pageObject.hasRightsHolderAccessCheckBox, hasAccess);
    }

    public checkStakeHolderAccessRoleStatusOnUser(email: string, hasAccess: boolean) {
        return this.assertCheckboxState(email, this.pageObject.hasStakeHolderAccessCheckBox, hasAccess);
    }

    private setUserCheckboxState(email: string, checkbox: protractor.ElementFinder, toState: boolean) {
        return this.openEditUser(email)
            .then(() => {
                return checkbox.isSelected()
                    .then(selected => {
                        if (selected !== toState) {
                            return checkbox.click()
                                .then(() => {
                                    return this.pageCreateObject.editUserButton.click();
                                });
                        } else {
                            return this.pageCreateObject.cancelEditUserButton.click();
                        }
                    });
            })
            .then(() => browser.waitForAngular());
    }

    public updateApiOnUser(email: string, apiAccess: boolean) {
        return this.setUserCheckboxState(email, this.pageObject.hasAPiCheckBox, apiAccess);
    }

    public updateRightsHolderAccessOnUser(email: string, hasAccess: boolean) {
        return this.setUserCheckboxState(email, this.pageObject.hasRightsHolderAccessCheckBox, hasAccess);
    }

    public updateStakeHolderAccessOnUser(email: string, hasAccess: boolean) {
        return this.setUserCheckboxState(email, this.pageObject.hasStakeHolderAccessCheckBox, hasAccess);
    }

    private getUserRow(email: string) {
        const emailColumnElementType = "userEmailObject";

        return this.pageObject.mainGridAllTableRows.filter((row, index) => {
            console.log("Searching for email column");
            var column = row.element(this.cssHelper.byDataElementType(emailColumnElementType));
            return column.isPresent()
                .then(present => {
                    if (present) {
                        console.log("Found email column - checking if row is the right one");
                        return column.getText()
                            .then(text => {
                                return text === email;
                            });
                    }
                    return false;
                });
        }).then(elements => {
            if (elements.length === 0) {
                throw `No rows found for email:${email}`;
            }
            return elements[0];
        });
    }

    private openEditUser(email: string) {
        return this.getUserRow(email)
            .then(row => {
                expect(row).not.toBe(null);
                return row.element(by.linkText("Redigér")).click();
            })
            .then(() => browser.waitForAngular());
{'fraction_non_alphanumeric': 0.10104712041884817, 'fraction_numerical': 0.0002617801047120419, 'mean_word_length': 2.091019417475728, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4434881', 'n_tokens_mistral': 1919, 'n_tokens_neox': 1852, 'n_words': 358}