Dduvall has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/172906

Change subject: Reimplemented API helper
......................................................................

Reimplemented API helper

Refactored `Environment#on_wiki` to derive an API URL (from the wiki
URL) when no explicit configuration for `:mediawiki_api_url` or the
equivalent for the given alternative is found.

Refactored ApiHelper mixin to use `Environment` methods.

Change-Id: I4c3ee50c1f1533e3c802f53603c8d4761cbb18c3
---
M lib/mediawiki_selenium/environment.rb
M lib/mediawiki_selenium/support/modules/api_helper.rb
A spec/api_helper_spec.rb
M spec/environment_spec.rb
4 files changed, 124 insertions(+), 16 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/selenium 
refs/changes/06/172906/1

diff --git a/lib/mediawiki_selenium/environment.rb 
b/lib/mediawiki_selenium/environment.rb
index 8810728..87260e9 100644
--- a/lib/mediawiki_selenium/environment.rb
+++ b/lib/mediawiki_selenium/environment.rb
@@ -195,6 +195,9 @@
     # Executes the given block within the context of an environment that's
     # using the given alternative wiki URL and its corresponding API endpoint.
     #
+    # If no API URL is explicitly defined for the given alternative, one is
+    # constructed relative to the wiki URL.
+    #
     # @example Visit a random page on wiki B
     #   on_wiki(:b) { visit(RandomPage) }
     #
@@ -207,7 +210,10 @@
     # @return [Environment]
     #
     def on_wiki(id, &blk)
-      with_alternative([:mediawiki_url, :mediawiki_api_url], id, &blk)
+      url = lookup(:mediawiki_url, id: id)
+      api_url = lookup(:mediawiki_api_url, id: id, default: api_url_from(url))
+
+      with(mediawiki_url: url, mediawiki_api_url: api_url, &blk)
     end
 
     # Returns the current value for `:mediawiki_password` or the value for the
@@ -218,7 +224,7 @@
     # @return [String]
     #
     def password(id = nil)
-      lookup(password_variable, id)
+      lookup(password_variable, id: id)
     end
 
     # Whether this environment has been configured to use remote browser
@@ -271,7 +277,7 @@
     # @return [String]
     #
     def user(id = nil)
-      lookup(:mediawiki_user, id)
+      lookup(:mediawiki_user, id: id)
     end
 
     # Returns the current user, or the one for the given alternative, with all
@@ -362,6 +368,10 @@
 
     private
 
+    def api_url_from(wiki_url)
+      URI.parse(wiki_url).merge("/w/api.php").to_s
+    end
+
     def browser_config
       lookup_all(browser_factory.all_binding_keys, default: nil).reject { |k, 
v| v.nil? }
     end
diff --git a/lib/mediawiki_selenium/support/modules/api_helper.rb 
b/lib/mediawiki_selenium/support/modules/api_helper.rb
index a4c0c1c..e630bf8 100644
--- a/lib/mediawiki_selenium/support/modules/api_helper.rb
+++ b/lib/mediawiki_selenium/support/modules/api_helper.rb
@@ -5,16 +5,19 @@
   # definitions.
   #
   module ApiHelper
-    # A pre-authenticated API client.
+    # An authenticated MediaWiki API client.
     #
     # @return [MediawikiApi::Client]
     #
     def api
-      return @api if defined?(@api)
+      @api_cache ||= {}
 
-      @api = MediawikiApi::Client.new(ENV["MEDIAWIKI_API_URL"])
-      @api.log_in(*ENV.values_at("MEDIAWIKI_USER", "MEDIAWIKI_PASSWORD")) 
unless @api.logged_in?
-      @api
+      url = lookup(:mediawiki_api_url, default: 
api_url_from(lookup(:mediawiki_url)))
+
+      @api_cache[url] ||= MediawikiApi::Client.new(url).tap do |client|
+        client.log_in(user, password)
+      end
     end
+
   end
 end
diff --git a/spec/api_helper_spec.rb b/spec/api_helper_spec.rb
new file mode 100644
index 0000000..a331d9d
--- /dev/null
+++ b/spec/api_helper_spec.rb
@@ -0,0 +1,76 @@
+require "spec_helper"
+
+module MediawikiSelenium
+  describe ApiHelper do
+    let(:env) { Environment.new(config).extend(ApiHelper) }
+
+    let(:config) do
+      {
+        mediawiki_url: "http://an.example/wiki/";,
+        mediawiki_api_url: api_url,
+        mediawiki_api_url_b: alternative_api_url,
+        mediawiki_user: "mw user",
+        mediawiki_password: "mw password"
+      }
+    end
+
+    let(:api_url) { "http://an.example/api"; }
+    let(:alternative_api_url) { "" }
+
+    describe "#api" do
+      subject { env.api }
+
+      let(:client) { double(MediawikiApi::Client) }
+
+      before do
+        expect(MediawikiApi::Client).to 
receive(:new).with(api_url).and_return(client)
+        expect(client).to receive(:log_in).with("mw user", "mw password")
+      end
+
+      context "called for the first time" do
+        it "returns a new client" do
+          expect(subject).to be(client)
+        end
+      end
+
+      context "called subsequently" do
+        before do
+          env.api
+        end
+
+        it "returns a cached client" do
+          expect(subject).to be(client)
+        end
+
+        context "from an altered environment" do
+          subject { env2.api }
+
+          let(:env2) { env.with_alternative(:mediawiki_api_url, :b) }
+
+          context "with the same API URL" do
+            let(:alternative_api_url) { api_url }
+
+            it "returns a cached client" do
+              expect(subject).to be(client)
+            end
+          end
+
+          context "with a different API URL" do
+            let(:alternative_api_url) { "http://another.example/api"; }
+
+            let(:client2) { double(MediawikiApi::Client) }
+
+            before do
+              expect(MediawikiApi::Client).to 
receive(:new).with(alternative_api_url).and_return(client2)
+              expect(client2).to receive(:log_in).with("mw user", "mw 
password")
+            end
+
+            it "returns a new client" do
+              expect(subject).not_to be(client)
+            end
+          end
+        end
+      end
+    end
+  end
+end
diff --git a/spec/environment_spec.rb b/spec/environment_spec.rb
index 1c3f65c..d471463 100644
--- a/spec/environment_spec.rb
+++ b/spec/environment_spec.rb
@@ -265,14 +265,17 @@
     end
 
     describe "#on_wiki" do
-      subject { env.on_wiki(:b) {} }
+      subject { env.on_wiki(id) {} }
+
+      let(:id) { :b }
 
       let(:config) do
         {
           mediawiki_url: "http://an.example/wiki";,
-          mediawiki_url_b: "http://alt.example/wiki";,
-          mediawiki_api_url: "http://an.example/api";,
-          mediawiki_api_url_b: "http://alt.example/api";,
+          mediawiki_api_url: "http://an.example/w/api.php";,
+          mediawiki_url_b: "http://altb.example/wiki";,
+          mediawiki_api_url_b: "http://altb.example/w/api.php";,
+          mediawiki_url_c: "http://altc.example/wiki";,
         }
       end
 
@@ -286,15 +289,31 @@
 
       it "executes in the new environment using the alternative wiki and API 
urls" do
         expect(new_config).to receive(:merge!).with(
-          mediawiki_url: "http://alt.example/wiki";,
-          mediawiki_api_url: "http://alt.example/api";
+          mediawiki_url: "http://altb.example/wiki";,
+          mediawiki_api_url: "http://altb.example/w/api.php";
         )
         expect(new_env).to receive(:instance_exec).with(
-          "http://alt.example/wiki";,
-          "http://alt.example/api";
+          "http://altb.example/wiki";,
+          "http://altb.example/w/api.php";
         )
         subject
       end
+
+      context "and no explicit API URL is configured for the wiki" do
+        let(:id) { :c }
+
+        it "constructs one relative to the wiki URL" do
+          expect(new_config).to receive(:merge!).with(
+            mediawiki_url: "http://altc.example/wiki";,
+            mediawiki_api_url: "http://altc.example/w/api.php";
+          )
+          expect(new_env).to receive(:instance_exec).with(
+            "http://altc.example/wiki";,
+            "http://altc.example/w/api.php";
+          )
+          subject
+        end
+      end
     end
 
     describe "#wiki_url" do

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I4c3ee50c1f1533e3c802f53603c8d4761cbb18c3
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/selenium
Gerrit-Branch: env-abstraction-layer
Gerrit-Owner: Dduvall <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to