Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run the E2E tests as a user with the author role #11359

Merged
merged 8 commits into from
Nov 7, 2018

Conversation

notnownikki
Copy link
Member

@notnownikki notnownikki commented Nov 1, 2018

Description

Adds support for E2E_ROLE env var which allows the e2e tests to run as a user with the author role.

It adds a dummy favicon.ico so that we don't (sometimes) error on the 404 when looking at front end posts, and ignores net::ERR_UNKNOWN_URL_SCHEME console message which we also (sometimes) get when viewing a front end post.

The code that deals with plugins switches to the admin user, and back to the test user after the plugin operations are done.

How has this been tested?

Both e2e tests should pass.

At the moment, they require #11395 because the fail is catching that bug.

Types of changes

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.

@notnownikki notnownikki added the [Status] In Progress Tracking issues with work in progress label Nov 1, 2018
@@ -94,7 +94,7 @@ describe( 'Change detection', () => {
// Toggle post as sticky (not persisted for autosave).
await ensureSidebarOpened();

const postStickyToggleButton = ( await page.$x( "//label[contains(text(), 'Stick to the Front Page')]" ) )[ 0 ];
const postStickyToggleButton = ( await page.$x( "//label[contains(text(), 'Pending Review')]" ) )[ 0 ];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Authors can mark things needing review, they can't stick things to the front page. The effect in the test is the same.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm reading correctly, the E2E tests can still be run by a non-author, in which case the label would not have the changed effect here. Should this be made more generic? Lookup element by its class? A helper utility which returns the button accounting for role?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can see, no matter what your user role, clicking "Pending Review" marks the post as dirty, and that's what's getting tested here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I think I misunderstood the reason for the change. It’s just changing it to an option which is visible for both admins and authors.

@@ -60,12 +62,12 @@ describe( 'templates', () => {
const STANDARD_FORMAT_VALUE = '0';

async function setPostFormat( format ) {
await switchToAdminUser();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switching user will not actually do anything if we're running as the admin user. We'll only switch if we're running as a different user, because the author role cannot manage plugins.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's good to know; could you leave a comment explaining that in the code? 😄

@@ -116,6 +116,12 @@ function observeConsoleLogging() {
return;
}

// Viewing posts on the front end can result in this error, which
// has nothing to do with Gutenberg.
if ( text.includes( 'net::ERR_UNKNOWN_URL_SCHEME' ) ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be a jQuery thing that happens on the front end, and we sometimes would get hit by it, but sometimes the test assertions would complete quickly enough for it not to make the test fail.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an issue anywhere that tracks that? I don't quite follow.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what causes it yet, it seems to be unrelated to Gutenberg, and happens when you view the published post on the front end. I'll investigate further after this merge.

@notnownikki notnownikki added [Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests. and removed [Status] In Progress Tracking issues with work in progress labels Nov 2, 2018
aduth
aduth previously requested changes Nov 2, 2018
@@ -94,7 +94,7 @@ describe( 'Change detection', () => {
// Toggle post as sticky (not persisted for autosave).
await ensureSidebarOpened();

const postStickyToggleButton = ( await page.$x( "//label[contains(text(), 'Stick to the Front Page')]" ) )[ 0 ];
const postStickyToggleButton = ( await page.$x( "//label[contains(text(), 'Pending Review')]" ) )[ 0 ];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm reading correctly, the E2E tests can still be run by a non-author, in which case the label would not have the changed effect here. Should this be made more generic? Lookup element by its class? A helper utility which returns the button accounting for role?

@@ -94,8 +94,8 @@ describe( 'Change detection', () => {
// Toggle post as sticky (not persisted for autosave).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're no longer toggling as sticky.

@notnownikki
Copy link
Member Author

Not sure what's going on here, I've merged master but the test still fails for authors when it passes on my local branch.

@notnownikki notnownikki force-pushed the update/e2e-author-role-test branch from 66cd0f1 to bd386ab Compare November 5, 2018 10:41
@notnownikki
Copy link
Member Author

So this is all working correctly now, and is catching an actual bug.

To recreate:

Set up a brand new WP install.
Set up an author user.
Create a new post, publish it.
Create a new post and add a "Latest Posts" block.
See the 403 error in the console.

Seems that the Latest Posts block is doing something with category information, and requesting /index.php?rest_route=%2Fwp%2Fv2%2Fcategories&per_page=100&context=edit&_locale=user which an author cannot access.

cc: @youknowriad @danielbachhuber

@youknowriad
Copy link
Contributor

@notnownikki This is probably a similar bug to the one fixed in #11395, we should use apiFetch directly in the block.

@notnownikki
Copy link
Member Author

@aduth I think I addressed your points here, and this would be good to get in because it caught a real bug.

@youknowriad Change to the Latest Posts block is included here now.

this.toggleDisplayPostDate = this.toggleDisplayPostDate.bind( this );
}

componentWillMount() {
apiFetch( {
path: addQueryArgs( `/wp/v2/categories`, CATEGORIES_LIST_QUERY ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this need to iterate the entire list? What if I have 102 categories?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤷‍♀️ this is just how it was in the original code, I was just fixing it so that it didn't break for authors

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I'll mention on #10873 and we can resolve with that.

path: addQueryArgs( `/wp/v2/categories`, CATEGORIES_LIST_QUERY ),
} ).then(
( categoriesList ) => {
this.setState( { categoriesList } );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the block is removed at this point, we should guard against it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an abort call when the component unmounts, same as the hierarchical term selector.

@@ -56,6 +56,10 @@ class LatestPostsEdit extends Component {
);
}

componentWillUnmount() {
invoke( this.fetchRequest, [ 'abort' ] );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we support "abort" in apiFetch requests?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we don't, then the hierarchical term selector is wrong too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we do :). I think we did at some point in apiRequest because we were relying on jQuery but it's not the case anymore AFAIK.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, no abort! Ok, I'll do the this.isStillMounted thing instead. Can we fix the term selector in another PR? I'm keen to get this in with as few changes as possible so authors get tested asap

@danielbachhuber danielbachhuber added this to the 4.3 milestone Nov 6, 2018
@aduth aduth dismissed their stale review November 6, 2018 20:36

Feedback addressed

Copy link
Member

@tofumatt tofumatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me and is a solid improvement.

I left a few comments and I'm not sure if other comments are resolved (they aren't marked as such automatically by GitHub, so I've flagged @aduth for another review). Never mind, I think maybe I had this tab left open and missed the feedback being addressed. :shipit:

I think this is a solid improvement though, cheers!

@@ -67,5 +67,9 @@ jobs:
- stage: test
env: WP_VERSION=latest
script:
- npm install || exit 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I presume this is run by Travis and thus not needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's run as part of the e2e tests. It didn't used to be, but now it is.

@@ -7,5 +7,8 @@ cd "$(dirname "$0")/../"
# Setup local environement
( ./bin/setup-local-env.sh )

# Run the tests
npm run test-e2e
if [ "$E2E_ROLE" = "author" ]; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the role we want to use could be passed to the script rather than hardcoding an "author-or-admin" choice, but if you want to do that as a follow-up it's fine with me 😄

(It would also let you avoid the if/else here, and instead just pass the role we want, with "admin" being the default. 🤷‍♂️)

this.isStillMounted = true;
this.fetchRequest = apiFetch( {
path: addQueryArgs( `/wp/v2/categories`, CATEGORIES_LIST_QUERY ),
} ).then(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if this request fails? We don't use catch here...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a catch which sets the list to an empty list.

@@ -60,12 +62,12 @@ describe( 'templates', () => {
const STANDARD_FORMAT_VALUE = '0';

async function setPostFormat( format ) {
await switchToAdminUser();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's good to know; could you leave a comment explaining that in the code? 😄

@@ -93,9 +97,13 @@ describe( 'templates', () => {
} );

it( 'should not populate new page with default block for format', async () => {
// This test always needs to run as the admin user.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain why it needs to run as the admin user?

@@ -116,6 +116,12 @@ function observeConsoleLogging() {
return;
}

// Viewing posts on the front end can result in this error, which
// has nothing to do with Gutenberg.
if ( text.includes( 'net::ERR_UNKNOWN_URL_SCHEME' ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an issue anywhere that tracks that? I don't quite follow.

@tofumatt tofumatt requested review from aduth and removed request for aduth November 6, 2018 23:06
@notnownikki notnownikki merged commit d6ba8c2 into master Nov 7, 2018
@notnownikki notnownikki deleted the update/e2e-author-role-test branch November 7, 2018 09:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants