Skip to content

Commit

Permalink
Add Client#create_account
Browse files Browse the repository at this point in the history
This brings us to feature parity with the old lib.

Change-Id: Ib3b722cee626301ff558cd3e26bbfa63ab2cc6b5
  • Loading branch information
Juliusz Gonera authored and zeljkofilipin committed Mar 13, 2014
1 parent 7be1198 commit 402740c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
20 changes: 20 additions & 0 deletions lib/mediawiki_api/client.rb
Expand Up @@ -6,6 +6,9 @@ module MediawikiApi
class LoginError < StandardError
end

class CreateAccountError < StandardError
end

class TokenError < StandardError
end

Expand Down Expand Up @@ -39,6 +42,23 @@ def log_in(username, password, token = nil)
end
end

def create_account(username, password, token = nil)
params = { action: "createaccount", name: username, password: password, format: "json" }
params[:token] = token unless token.nil?
resp = @conn.post "", params

data = JSON.parse(resp.body)["createaccount"]

case data["result"]
when "Success"
@logged_in = true
when "NeedToken"
create_account username, password, data["token"]
else
raise CreateAccountError, data["result"]
end
end

def create_page(title, content)
token = get_token "edit"
resp = @conn.post "", { action: "edit", title: title, text: content, token: token, format: "json" }
Expand Down
53 changes: 52 additions & 1 deletion spec/client_spec.rb
Expand Up @@ -2,9 +2,9 @@

describe MediawikiApi::Client do
subject { MediawikiApi::Client.new(api_url) }
body_base = { cookieprefix: "prefix", sessionid: "123" }

describe "#log_in" do
body_base = { cookieprefix: "prefix", sessionid: "123" }

it "logs in when API returns Success" do
stub_request(:post, api_url).
Expand Down Expand Up @@ -100,4 +100,55 @@

# evaluate results
end

describe "#create_account" do
it "creates an account when API returns Success" do
stub_request(:post, api_url).
with(body: { format: "json", action: "createaccount", name: "Test", password: "qwe123" }).
to_return(body: { createaccount: body_base.merge({ result: "Success" }) }.to_json )

subject.create_account("Test", "qwe123").should be true
end

context "when API returns NeedToken" do
before do
headers = { "Set-Cookie" => "prefixSession=789; path=/; domain=localhost; HttpOnly" }

stub_request(:post, api_url).
with(body: { format: "json", action: "createaccount", name: "Test", password: "qwe123" }).
to_return(
body: { createaccount: body_base.merge({ result: "NeedToken", token: "456" }) }.to_json,
headers: { "Set-Cookie" => "prefixSession=789; path=/; domain=localhost; HttpOnly" }
)

@success_req = stub_request(:post, api_url).
with(body: { format: "json", action: "createaccount", name: "Test", password: "qwe123", token: "456" }).
with(headers: { "Cookie" => "prefixSession=789" }).
to_return(body: { createaccount: body_base.merge({ result: "Success" }) }.to_json )
end

it "creates an account" do
subject.create_account("Test", "qwe123").should be true
end

it "sends second request with token and cookies" do
subject.create_account "Test", "qwe123"
@success_req.should have_been_requested
end
end

# docs don't specify other results, but who knows
# http://www.mediawiki.org/wiki/API:Account_creation
context "when API returns neither Success nor NeedToken" do
before do
stub_request(:post, api_url).
with(body: { format: "json", action: "createaccount", name: "Test", password: "qwe123" }).
to_return(body: { createaccount: body_base.merge({ result: "WhoKnows" }) }.to_json )
end

it "raises error with proper message" do
expect { subject.create_account "Test", "qwe123" }.to raise_error MediawikiApi::CreateAccountError, "WhoKnows"
end
end
end
end

0 comments on commit 402740c

Please sign in to comment.