EBernhardson has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/381359 )

Change subject: Convert integration test api objects to single use
......................................................................

Convert integration test api objects to single use

There are some annoying problems with re-using api clients
around api tokens, namely that the tokens can be accidentally
overwritten if multiple things are using it asyncronously.

Convert to a more direct model where new api clients are created
on demand. Also makes it a bit clearer how to do things on multiple
wikis with the api. Multiple wikis on the browser is not handled
here.

Change-Id: I06519bc43f8c17d81920f6210ea2deb1b310d5e4
---
M tests/integration/features/step_definitions/page_step_helpers.js
M tests/integration/features/support/hooks.js
M tests/integration/features/support/world.js
3 files changed, 56 insertions(+), 48 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CirrusSearch 
refs/changes/59/381359/1

diff --git a/tests/integration/features/step_definitions/page_step_helpers.js 
b/tests/integration/features/step_definitions/page_step_helpers.js
index 4af1791..2ca50c6 100644
--- a/tests/integration/features/step_definitions/page_step_helpers.js
+++ b/tests/integration/features/step_definitions/page_step_helpers.js
@@ -13,34 +13,45 @@
 const expect = require( 'chai' ).expect;
 
 class StepHelpers {
-       constructor( world ) {
+       constructor( world, wiki ) {
                this.world = world;
+               this.apiPromise = world.onWiki( wiki || 
world.config.wikis.default );
+       }
+
+       onWiki( wiki ) {
+               return new StepHelpers( this.world, wiki );
        }
 
        deletePage( title ) {
-               return this.world.apiClient.loginAndEditToken().then( () => {
-                       return this.world.apiClient.delete( title, 
"CirrusSearch integration test delete" )
-                               .catch( ( err ) => {
-                                       // still return true if page doesn't 
exist
-                                       return expect( err.message 
).to.include( "doesn't exist" );
-                               } );
+               return this.apiPromise.then( ( api ) => {
+                       return api.loginGetEditToken().then( () => {
+                               return api.delete( title, "CirrusSearch 
integration test delete" )
+                                       .catch( ( err ) => {
+                                               // still return true if page 
doesn't exist
+                                               return expect( err.message 
).to.include( "doesn't exist" );
+                                       } );
+                       } );
                } );
        }
        editPage( title, content ) {
-               return this.world.apiClient.loginAndEditToken().then( () => {
-                       return this.world.apiClient.edit( title, content, 
"CirrusSearch integration test edit" );
+               return this.apiPromise.then( ( api ) => {
+                       return api.loginGetEditToken().then( () => {
+                               return api.edit( title, content, "CirrusSearch 
integration test edit" );
+                       } );
                } );
        }
 
        suggestionSearch( query, limit = 'max' ) {
-               return this.world.apiClient.request( {
-                       action: 'opensearch',
-                       search: query,
-                       cirrusUseCompletionSuggester: 'yes',
-                       limit: limit
+               return this.apiPromise.then( ( api ) => {
+                       return api.request( {
+                               action: 'opensearch',
+                               search: query,
+                               cirrusUseCompletionSuggester: 'yes',
+                               limit: limit
+                       } );
                } ).then( ( response ) => this.world.setApiResponse( response ) 
);
        }
 
 }
 
-module.exports = StepHelpers;
\ No newline at end of file
+module.exports = StepHelpers;
diff --git a/tests/integration/features/support/hooks.js 
b/tests/integration/features/support/hooks.js
index bb96f46..26e3dcb 100644
--- a/tests/integration/features/support/hooks.js
+++ b/tests/integration/features/support/hooks.js
@@ -53,9 +53,10 @@
                                "はーい": "makes sure we do not fail to index 
empty tokens (T156234)"
                        }
                };
-               return this.apiClient.loginAndEditToken().then( () => {
-                       return this.apiClient.batch(batchJobs, 'CirrusSearch 
integration test edit');
+               return this.onWiki().then( ( api ) => {
+                       return api.loginGetEditToken().then( () => {
+                               return api.batch(batchJobs, 'CirrusSearch 
integration test edit');
+                       } );
                } );
        } );
-
 } );
diff --git a/tests/integration/features/support/world.js 
b/tests/integration/features/support/world.js
index 06c5b1c..f362144 100644
--- a/tests/integration/features/support/world.js
+++ b/tests/integration/features/support/world.js
@@ -1,4 +1,5 @@
 /*jshint esversion: 6, node:true */
+/*global browser, console */
 
 /**
  * The World is a container for global state shared across test steps.
@@ -50,11 +51,6 @@
        this.attach = attach;
        this.parameters = parameters;
 
-       // Binding step helpers to this World.
-       // Step helpers are just step functions that are abstracted
-       // for the purpose of using them outside of the steps themselves (like 
in hooks).
-       this.stepHelpers = new StepHelpers( this );
-
        // Since you can't pass values between step definitions directly,
        // the last Api response is stored here so it can be accessed between 
steps.
        // (I have a feeling this is prone to race conditions).
@@ -72,33 +68,34 @@
        // Extra process tracking what tags have been initialized
        this.checkTag = tagClient.check.bind( tagClient );
 
+       // Per-wiki api clients
        this.onWiki = function( wiki = this.config.wikis.default ) {
                let w = this.config.wikis[ wiki ];
-               return {
-                       username: w.username,
-                       password: w.password,
+               let client = new Bot();
+               client.setOptions({
+                       verbose: true,
+                       silent: false,
+                       defaultSummary: 'MWBot',
+                       concurrency: 1,
                        apiUrl: w.apiUrl
+               });
+               let origLoginGetEditToken = client.loginGetEditToken;
+               client.loginGetEditToken = function () {
+                       return origLoginGetEditToken.call( client, {
+                               username: w.username,
+                               password: w.password,
+                               apiUrl: w.apiUrl
+                       } );
                };
-        };
-
-       // Instanciates new `mwbot` Api Client
-       this.apiClient = new Bot();
-       this.apiClient.setOptions({
-               verbose: true,
-               silent: false,
-               defaultSummary: 'MWBot',
-               concurrency: 1,
-               apiUrl: this.onWiki().apiUrl
-        });
-
-       /**
-        * Shortcut to `loginGetEditToken` that sets a default parameter for 
login.
-        * Hopefully `mwbot` can handle multiple loggins at once (not yet 
tested).
-        */
-       this.apiClient.loginAndEditToken = ( wiki = this.config.wikis.default ) 
=> {
-               let w = this.onWiki( wiki );
-               return this.apiClient.loginGetEditToken( w );
+               return new Promise( ( resolve ) => {
+                       resolve( client );
+               } );
        };
+
+       // Binding step helpers to this World.
+       // Step helpers are just step functions that are abstracted
+       // for the purpose of using them outside of the steps themselves (like 
in hooks).
+       this.stepHelpers = new StepHelpers( this );
 
        // Shortcut for browser.url(), accepts a Page object
        // as well as a string, assumes the Page object
@@ -118,10 +115,9 @@
                browser.url( tmpUrl );
                // logs full URL in case of typos, misplaced backslashes.
                console.log( `Visited page: ${browser.getUrl()}` );
-
        };
-}
+};
 
 defineSupportCode( function( { setWorldConstructor } ) {
        setWorldConstructor( World );
-});
\ No newline at end of file
+});

-- 
To view, visit https://gerrit.wikimedia.org/r/381359
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I06519bc43f8c17d81920f6210ea2deb1b310d5e4
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/CirrusSearch
Gerrit-Branch: master
Gerrit-Owner: EBernhardson <ebernhard...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to