From 819f975948be7f95589949814472d82a8245b535 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Mon, 19 Feb 2018 17:18:40 -0800 Subject: [PATCH] add a user role --- tests/roles.js | 18 ++++++++++++++++++ tests/spec/02-login-spec.js | 8 +++++--- tests/spec/03-basic-timeline-spec.js | 20 +++++++++++--------- tests/utils.js | 14 -------------- 4 files changed, 34 insertions(+), 26 deletions(-) create mode 100644 tests/roles.js diff --git a/tests/roles.js b/tests/roles.js new file mode 100644 index 00000000..814d28a6 --- /dev/null +++ b/tests/roles.js @@ -0,0 +1,18 @@ +import { Selector as $, Role } from 'testcafe' +import { addInstanceButton, getUrl, instanceInput } from './utils' + +function login(t, username, password) { + return t.typeText(instanceInput, 'localhost:3000') + .click(addInstanceButton) + .expect(getUrl()).eql('http://localhost:3000/auth/sign_in') + .typeText($('input#user_email'), username) + .typeText($('input#user_password'), password) + .click($('button[type=submit]')) + .expect(getUrl()).contains('/oauth/authorize') + .click($('button[type=submit]:not(.negative)')) + .expect(getUrl()).eql('http://localhost:4002/') +} + +export const foobarRole = Role('http://localhost:4002/settings/instances/add', async t => { + await login(t, 'foobar@localhost:3000', 'foobarfoobar') +}) \ No newline at end of file diff --git a/tests/spec/02-login-spec.js b/tests/spec/02-login-spec.js index 92b37a15..a118f2f2 100644 --- a/tests/spec/02-login-spec.js +++ b/tests/spec/02-login-spec.js @@ -1,13 +1,15 @@ import { Selector as $ } from 'testcafe' import { addInstanceButton, getUrl, instanceInput, login, settingsButton } from '../utils' +import { foobarRole } from '../roles' fixture `02-login-spec.js` .page `http://localhost:4002` const formError = $('.form-error') -test('Cannot log in to a fake instance', async t => { +test('Cannot log in to a fake instance', async t => { await t.click($('a').withText('log in to an instance')) + .expect(getUrl()).contains('/settings/instances/add') .typeText(instanceInput, 'fake.nolanlawson.com') .click(addInstanceButton) .expect(formError.exists).ok() @@ -20,12 +22,12 @@ test('Cannot log in to a fake instance', async t => { }) test('Logs in to localhost:3000', async t => { - await login(t, 'foobar@localhost:3000', 'foobarfoobar') + await t.useRole(foobarRole) .expect($('article.status-article').exists).ok() }) test('Logs out', async t => { - await login(t, 'foobar@localhost:3000', 'foobarfoobar') + await t.useRole(foobarRole) .click(settingsButton) .click($('a').withText('Instances')) .click($('a').withText('localhost:3000')) diff --git a/tests/spec/03-basic-timeline-spec.js b/tests/spec/03-basic-timeline-spec.js index c79b2bfe..d21c6316 100644 --- a/tests/spec/03-basic-timeline-spec.js +++ b/tests/spec/03-basic-timeline-spec.js @@ -1,17 +1,15 @@ import { Selector as $ } from 'testcafe' import { getUrl, login, validateTimeline } from '../utils' import { homeTimeline, notifications, localTimeline, favorites } from '../fixtures' +import { foobarRole } from '../roles' fixture `03-basic-timeline-spec.js` .page `http://localhost:4002` - .beforeEach(async t => { - await login(t, 'foobar@localhost:3000', 'foobarfoobar') - }) const firstArticle = $('.virtual-list-item[aria-hidden=false] .status-article') test('Shows the home timeline', async t => { - await t + await t.useRole(foobarRole) .expect(firstArticle.hasAttribute('aria-setsize')).ok() .expect(firstArticle.getAttribute('aria-posinset')).eql('0') @@ -21,21 +19,24 @@ test('Shows the home timeline', async t => { }) test('Shows notifications', async t => { - await t.click($('nav a[aria-label=Notifications]')) + await t.useRole(foobarRole) + .click($('nav a[aria-label=Notifications]')) .expect(getUrl()).contains('/notifications') await validateTimeline(t, notifications) }) test('Shows the local timeline', async t => { - await t.click($('nav a[aria-label=Local]')) - await t.expect(getUrl()).contains('/local') + await t.useRole(foobarRole) + .click($('nav a[aria-label=Local]')) + .expect(getUrl()).contains('/local') await validateTimeline(t, localTimeline) }) test('Shows the federated timeline', async t => { - await t.click($('nav a[aria-label=Community]')) + await t.useRole(foobarRole) + .click($('nav a[aria-label=Community]')) .expect(getUrl()).contains('/community') .click($('a').withText('Federated')) .expect(getUrl()).contains('/federated') @@ -44,7 +45,8 @@ test('Shows the federated timeline', async t => { }) test('Shows favorites', async t => { - await t.click($('nav a[aria-label=Community]')) + await t.useRole(foobarRole) + .click($('nav a[aria-label=Community]')) .expect(getUrl()).contains('/community') .click($('a').withText('Favorites')) .expect(getUrl()).contains('/favorites') diff --git a/tests/utils.js b/tests/utils.js index 11bfb2d0..112420ec 100644 --- a/tests/utils.js +++ b/tests/utils.js @@ -6,20 +6,6 @@ export const addInstanceButton = $('.add-new-instance button') export const getUrl = exec(() => window.location.href) -export function login(t, username, password) { - return t.click($('a').withText('log in to an instance')) - .expect(getUrl()).contains('/settings/instances/add') - .typeText(instanceInput, 'localhost:3000') - .click(addInstanceButton) - .expect(getUrl()).eql('http://localhost:3000/auth/sign_in') - .typeText($('input#user_email'), username) - .typeText($('input#user_password'), password) - .click($('button[type=submit]')) - .expect(getUrl()).contains('/oauth/authorize') - .click($('button[type=submit]:not(.negative)')) - .expect(getUrl()).eql('http://localhost:4002/') -} - export function getNthVirtualArticle (n) { return $(`.virtual-list-item[aria-hidden="false"] article[aria-posinset="${n}"]`) }