Skip to content

Commit

Permalink
added relative urls for Role constructor (#8018)
Browse files Browse the repository at this point in the history
## Purpose
Can't specify relative login page URLs in the Role constructor

## Approach
Role's url check has been moved to before initialization function.
If configuration file or cli command include parameter "baseUrl", user
can use relative urls in Role constructor.

## References
[issues 8012](#8012)
  • Loading branch information
PavelMor25 authored Oct 16, 2023
1 parent 31b97c1 commit 4897fbc
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/api/test-page-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function getUrl (url: string, base?: URL): string {
return resolveRelativeUrl(url, base);
}

else if (isAbsolute(url))
if (isAbsolute(url))
return pathToFileURL(url).toString();

return ensureProtocol(url);
Expand Down
2 changes: 1 addition & 1 deletion src/errors/runtime/templates.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions src/role/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { assertType, is } from '../errors/runtime/type-assertions';
import wrapTestFunction from '../api/wrap-test-function';
import { getUrl, assertRoleUrl } from '../api/test-page-url';
import Role from './role';

interface RoleOptions {
Expand All @@ -15,9 +14,6 @@ export function createRole (loginUrl: string, initFn: Function, options: RoleOpt
if (options.preserveUrl !== void 0)
assertType(is.boolean, 'Role', 'The "preserveUrl" option', options.preserveUrl);

assertRoleUrl(loginUrl, 'Role');

loginUrl = getUrl(loginUrl);
initFn = wrapTestFunction(initFn);

return new Role(loginUrl, initFn, options);
Expand Down
11 changes: 11 additions & 0 deletions src/role/role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import roleMarker from './marker-symbol';
import { nanoid } from 'nanoid';
import TestRun from '../test-run';
import TestCafeErrorList from '../errors/error-list';
import { getUrl, assertRoleUrl } from '../api/test-page-url';

export interface RedirectUrl {
[testId: string]: string;
Expand Down Expand Up @@ -63,8 +64,18 @@ export default class Role extends EventEmitter {
}
}

private _prepareLoginUrl (loginUrl: string, baseUrl: string): string {
if (!baseUrl)
assertRoleUrl(loginUrl, 'role');

return getUrl(loginUrl, baseUrl ? new URL(baseUrl) : void 0);
}

private async _switchToCleanRun (testRun: TestRun): Promise<void> {
try {
if (this.loginUrl)
this.loginUrl = this._prepareLoginUrl(this.loginUrl, testRun.baseUrl);

await testRun.switchToCleanRun(this.loginUrl as string);
}
catch (err: any) {
Expand Down
4 changes: 4 additions & 0 deletions src/test-run/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,10 @@ export default class TestRun extends AsyncEventEmitter {
return this.driverTaskQueue[0];
}

public get baseUrl (): string {
return this.opts.baseUrl as string || '';
}

private _resolveCurrentDriverTask (result?: unknown): void {
this.currentDriverTask.resolve(result);
this.driverTaskQueue.shift();
Expand Down
4 changes: 2 additions & 2 deletions test/functional/fixtures/api/es-next/generic-errors/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ describe('[API] Generic errors', function () {
it('Should handle the relative path in the Role constructor', () => {
return runTests('testcafe-fixtures/role-initialized-with-relative-url.js', null,
{ shouldFail: true, only: 'chrome' })
.catch(err => {
expect(err.message).contains('You cannot specify relative login page URLs in the Role constructor. Use an absolute URL.');
.catch(errs => {
expect(errs[0]).contains('Your Role includes a relative login page URL, but the "baseUrl" option is not set.');
});
});

Expand Down
26 changes: 26 additions & 0 deletions test/functional/fixtures/api/es-next/roles/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,30 @@ const isRemoteTask = config.currentEnvironmentName === config.testingEnvironment
});
});
});

describe('URL is Role constructor', function () {
it('Should throw "error in role initializer" without baseUrl and with relative path Role', () => {
return runTests(
'./testcafe-fixtures/role-with-baseurl-test.js',
'Should throw error in role initializer without baseUrl and with relative path Role',
{ shouldFail: true })
.catch(errs => {
expect(errs[0]).contains('Your Role includes a relative login page URL, but the "baseUrl" option is not set.');
});
});

it('Should pass if `baseUrl` is set with relative path Role', () => {
return runTests(
'./testcafe-fixtures/role-with-baseurl-test.js',
'Use role with relative path and baseUrl',
{ baseUrl: 'http://localhost:3000/' });
});

it('Should pass if `baseUrl` is set with absolute path Role', () => {
return runTests(
'./testcafe-fixtures/role-with-baseurl-test.js',
'Use role with absolute path and baseUrl',
{ baseUrl: 'http://localhost:3000/' });
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Role, ClientFunction } from 'testcafe';

fixture `Roles with absolute/relative "loginUrl"`;

const getLocation = ClientFunction(() => document.location.toString());

const roleAbs = Role('http://localhost:3000/fixtures/api/es-next/roles/pages/index.html', async t => {
await t.expect(getLocation()).eql('http://localhost:3000/fixtures/api/es-next/roles/pages/index.html');
}, { preserveUrl: true });

const roleRel = Role('./fixtures/api/es-next/roles/pages/index.html', async t => {
await t.expect(getLocation()).eql('http://localhost:3000/fixtures/api/es-next/roles/pages/index.html');
}, { preserveUrl: true });

test('Should throw error in role initializer without baseUrl and with relative path Role', async t => {
await t.useRole(roleRel);
});

test('Use role with relative path and baseUrl', async t => {
await t.useRole(roleRel);
});

test('Use role with absolute path and baseUrl', async t => {
await t.useRole(roleAbs);
});

0 comments on commit 4897fbc

Please sign in to comment.