require File.dirname(__FILE__) + '/../../spec_helper.rb'
describe MediaWiki::Gateway do
def media_wiki_url
"#{LonelyPlanet::OnRails.environment.services[:atlas_mediawiki]}"
end
before do
@gateway = MediaWiki::Gateway.new({:url => media_wiki_url, :username => 'atlasmw', :password => 'wombat'})
$fake_media_wiki.reset
end
describe '.wiki_to_uri' do
it "should underscore spaces" do
MediaWiki.wiki_to_uri('getting there').should == 'getting_there'
end
it "should escape ampersands" do
MediaWiki.wiki_to_uri('getting there & away').should == 'getting_there_%26_away'
end
it "should escape UTF-8" do
MediaWiki.wiki_to_uri('Phở').should == 'Ph%E1%BB%9F'
end
it "should escape each path component but leave slashes untouched" do
MediaWiki.wiki_to_uri('Phở/B&r/B z').should == 'Ph%E1%BB%9F/B%26r/B_z'
end
it "should pass through nil" do
MediaWiki.wiki_to_uri(nil).should == nil
end
end
describe '#login' do
describe "with a valid username & password" do
before do
@gateway.login('atlasmw', 'wombat')
end
it "should login successfully" do
$fake_media_wiki.logged_in('atlasmw').should == true
end
end
describe "with an non-existent username" do
it "should raise an error" do
lambda do
@gateway.login('bogususer', 'sekrit')
end.should raise_error(StandardError)
end
end
describe "with an incorrect password" do
it "should raise an error" do
lambda do
@gateway.login('atlasmw', 'sekrit')
end.should raise_error(StandardError)
end
end
end
describe "#get_token" do
describe "when not logged in" do
describe "requesting an edit token" do
before do
@token = @gateway.get_token('edit', 'Main Page')
end
it "should return a blank token" do
@token.should_not == nil
@token.should == "+\\"
end
end
describe "requesting an import token" do
it "should raise an error" do
lambda do
@gateway.get_token('import', 'Main Page')
end.should raise_error(StandardError)
end
end
end
describe "when logged in as admin user" do
before do
@gateway.login('atlasmw', 'wombat')
end
describe "requesting an edit token for a single page" do
before do
@token = @gateway.get_token('edit', 'Main Page')
end
it "should return a token" do
@token.should_not == nil
@token.should_not == "+\\"
end
end
describe "requesting an edit token for multiple pages" do
before do
@token = @gateway.get_token('edit', "Main Page|Another Page")
end
it "should return a token" do
@token.should_not == nil
@token.should_not == "+\\"
end
end
describe "requesting an import token" do
before do
@token = @gateway.get_token('import', 'Main Page')
end
it "should return a token" do
@token.should_not == nil
@token.should_not == "+\\"
end
end
end
end
describe "#get" do
describe "for an existing wiki page" do
before do
$fake_media_wiki.add_page("page/title", "Wiki-text content")
end
it "returns raw page content" do
@gateway.get("page/title").should == "Wiki-text content"
end
end
describe "for a missing wiki page" do
it "returns nil" do
@gateway.get("page/missing").should be_nil
end
end
describe "for root (/)" do
it "returns nil" do
@gateway.get("").should be_nil
end
end
end
describe "#render" do
describe "for an existing wiki page" do
before do
@pages = @gateway.render('Main Page')
end
it "should return the page content" do
expected = "Sample HTML content"
@pages.to_s.should == expected
end
end
describe "for a missing wiki page" do
before do
@pages = @gateway.render('Invalidpage')
end
it "should return nil" do
@pages.should == nil
end
end
end
describe "#semantic_query" do
before do
@response = @gateway.semantic_query('[[place-id::123]]', ['mainlabel=Page'])
end
it "should return an HTML string" do
@response.should == "Sample HTML content"
end
end
describe "#create" do
before do
@gateway.login('atlasmw', 'wombat')
end
describe "when creating a new page" do
before do
@page = @gateway.create("A New Page", "Some content")
end
it "should create the page" do
expected = <<-XML
XML
Hash.from_xml(@page.to_s).should == Hash.from_xml(expected)
end
end
describe "when creating a page that already exists" do
before do
@existing_page = $fake_media_wiki.add_page("The Page", "Some content")
end
describe "and the 'overwrite' option is set" do
before do
@new_page = @gateway.create("The Page", "Some new content", :summary => "The summary", :overwrite => true)
end
it "should overwrite the existing page" do
expected = <<-XML
XML
Hash.from_xml(@new_page.to_s).should == Hash.from_xml(expected)
end
end
describe "and the 'overwrite' option is not set" do
it "should raise an error" do
lambda do
@gateway.create("The Page", "Some new content")
end.should raise_error(StandardError)
end
end
end
end
describe "#delete" do
describe "when logged in as admin" do
describe "and the page exists" do
def delete_response
<<-XML
XML
end
before do
@gateway.login("atlasmw", "wombat")
@gateway.create("Deletable Page", "Some content")
@page = @gateway.delete("Deletable Page")
end
it "should delete the page" do
Hash.from_xml(@page.to_s) == Hash.from_xml(delete_response)
end
end
describe "and the page does not exist" do
before do
@gateway.login("atlasmw", "wombat")
end
it "should raise an error" do
lambda do
@gateway.delete("Missing Page")
end.should raise_error(StandardError)
end
end
end
describe "when not logged in" do
before do
@gateway.create("Deletable Page", "Some content")
end
it "should raise an error" do
lambda do
@gateway.delete("Deletable Page")
end.should raise_error(StandardError)
end
end
end
describe "#import" do
def import_file
File.dirname(__FILE__) + "/import-test-data.xml"
end
describe "when not logged in" do
it "should raise an error" do
lambda do
@gateway.import(import_file)
end.should raise_error(StandardError)
end
end
describe "when logged in as admin" do
def import_response
<<-XML
XML
end
before do
@gateway.login("atlasmw", "wombat")
@page = @gateway.import(import_file)
end
it "should import content" do
Hash.from_xml(@page.to_s) == Hash.from_xml(import_response)
end
end
end
describe "#export" do
def export_data
page = $fake_media_wiki.page_by_title("Main Page")
<<-XML
#{page[:title]}
#{page[:pageid]}
#{page[:pageid]}
#{page[:content]}
XML
end
before do
@page = @gateway.export("Main Page")
end
it "should return export data for the page" do
Hash.from_xml(@page.to_s).should == Hash.from_xml(export_data)
end
end
describe "#list" do
before do
$fake_media_wiki.reset
end
describe "with an empty key" do
before do
@list = @gateway.list("")
end
it "should list all pages" do
@list.sort.should == [ "Book:Italy", "Level/Level/Index", "Main 2", "Main Page" ]
end
end
describe "with a namespace as the key" do
before do
@list = @gateway.list("Book:")
end
it "should list all pages in the namespace" do
@list.should == [ "Book:Italy" ]
end
end
describe "with a partial title as the key" do
before do
@list = @gateway.list("Main")
end
it "should list all pages in the main namespace that start with key" do
@list.sort.should == [ "Main 2", "Main Page" ]
end
end
end
describe "#search" do
before do
$fake_media_wiki.reset
@gateway.create("Search Test", "Foo KEY Blah")
@gateway.create("Search Test 2", "Zomp KEY Zorg")
@gateway.create("Book:Search Test", "Bar KEY Baz")
@gateway.create("Sandbox:Search Test", "Evil KEY Evil")
end
describe "with an empty key" do
it "should raise an error" do
lambda do
@gateway.search("")
end.should raise_error(StandardError)
end
end
describe "with a valid key and no namespaces" do
before do
@search = @gateway.search("KEY")
end
it "should list all matching pages in the main namespace" do
@search.should == [ "Search Test", "Search Test 2" ]
end
end
describe "with a valid key and a namespace string" do
before do
@search = @gateway.search("KEY", "Book")
end
it "should list all matching pages in the specified namespaces" do
@search.should == [ "Book:Search Test" ]
end
end
describe "with a valid key and a namespace array" do
before do
@search = @gateway.search("KEY", ["Book", "Sandbox"])
end
it "should list all matching pages in the specified namespaces" do
@search.should == [ "Sandbox:Search Test", "Book:Search Test" ]
end
end
end
describe "#namespaces_by_prefix" do
before do
$fake_media_wiki.reset
@namespaces = @gateway.namespaces_by_prefix
end
it "should list all namespaces" do
@namespaces.should == { "" => 0, "Book" => 100, "Sandbox" => 200}
end
end
describe "#extensions" do
before do
$fake_media_wiki.reset
@extensions = @gateway.extensions
end
it "should list all namespaces" do
@extensions.should == { "FooExtension" => "r1", "BarExtension" => "r2" }
end
end
end