question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement user roles

See original GitHub issue

Roles

Use roles to observe results or perform actions from different user perspectives. Also, it’s a solution for the forms authentication problem. Role initialization will be executed once per task on first demand and can be shared among tests and fixtures. Technically role saves created cookies and storages state. When we switch role in tests and if it’s not initialized yet then initializations steps run and we return back to the page on which we stopped execution. If it’s already initialized then page will be just reloaded with the new credentials.

helpers.js

import { Role } from 'testcafe';

export var registeredUser = Role(t => {
    await t
        .navigateTo('http://example.org')
        .typeText('#login', 'TonyStark')
        .typeText('#password', 'swordFish');
        .click('#login');
});;

test.js

import { registeredUser } from '../helpers';
import { Role } from 'testcafe';

fixture `example.org tests`;

test('Anonymous users can see newly created comments', async t => {
    await t
        .switchRole(registeredUser)
        .navigateTo('http://example.org')
        .typeText('#comment', 'Hey ya!')
        .click('#submit')
        .switchRole(Role.anonymous());

        var comment = await t.select('#comment-data');

        expect(comment.innerText).eql('Hey ya!');
});

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:12
  • Comments:27 (13 by maintainers)

github_iconTop GitHub Comments

2reactions
helen-dikarevacommented, Mar 13, 2017

What about t.ctx: it’ll be shared between test and role or not?

1reaction
inikulincommented, Mar 22, 2017

@p-bakker I can’t reproduce it on my side, this works fine for me:

import { t, Role, ClientFunction } from 'testcafe';

const getLocation = ClientFunction(() => location.href);

const someRole = Role('http://google.com', async () => {
    someRole.preservedUrl = await getLocation();
});

async function switchToRoleAndKeepUrl (role) {
    await t.useRole(role);

    console.log('session acivated', role.preservedUrl);

    if (role.preservedUrl)
        await t.navigateTo(role.preservedUrl);
}

fixture `Fixture`
    .page `https://devexpress.github.io/testcafe/example/`;

test('Test', async () => {
    await switchToRoleAndKeepUrl(someRole);
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Implementing Role Based Security in a Web App - Medium
Implementing role based security can be a large development effort with a significant amount of risk (denying access to users or opening up ......
Read more >
Role-Based Access Control - Auth0
You can also use roles to collect permissions defined for various APIs. For example, say you have a marketing module that allows users...
Read more >
Implementing Role Based Access Control in a Web Application
Define permissions based on resources and actions (i.e. users:create ). This leads to well-defined permissions that never need to change even if ...
Read more >
Introducing User Roles Authorization with SuperTokens
Part 2 - Implement SuperTokens User Roles · Step 1) Creating the roles and permissions · Step 2) Assigning roles to users on...
Read more >
What is Role-Based Access Control (RBAC)? - Varonis
5 Steps to Implement Role-Based Access Control ; Define the resources and services you provide to your users (e.g., email, CRM, file shares, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found