spec/lib/ezid/client_spec.rb in ezid-client-0.3.0 vs spec/lib/ezid/client_spec.rb in ezid-client-0.4.0
- old
+ new
@@ -1,90 +1,181 @@
module Ezid
RSpec.describe Client do
+
describe "initialization" do
describe "without a block" do
- it "should not be logged in" do
- expect(subject).not_to be_logged_in
+ it "should not login" do
+ expect_any_instance_of(described_class).not_to receive(:login)
+ described_class.new
end
end
- describe "with a block", :vcr do
- it "should be logged in" do
+ describe "with a block", type: :feature do
+ it "should wrap the block in a session" do
+ expect_any_instance_of(described_class).to receive(:login).and_call_original
+ expect_any_instance_of(described_class).to receive(:logout).and_call_original
described_class.new do |client|
- expect(client).to be_logged_in
+ expect(client.session).to be_open
end
end
end
end
- describe "authentication", :vcr do
- describe "logging in" do
- before { subject.login }
- it "should be logged in" do
- expect(subject).to be_logged_in
+
+ describe "authentication", type: :feature do
+ describe "#login" do
+ it "should open a session" do
+ expect(subject.session).to be_closed
+ subject.login
+ expect(subject.session).to be_open
end
end
- describe "logging out" do
+ describe "#logout" do
before { subject.login }
- it "should not be logged in" do
+ it "should close the session" do
+ expect(subject.session).to be_open
subject.logout
- expect(subject).not_to be_logged_in
+ expect(subject.session).to be_closed
end
end
+ describe "without a session" do
+ it "should send the user name and password" do
+ expect_any_instance_of(Net::HTTP::Post).to receive(:basic_auth).with(subject.user, subject.password).and_call_original
+ subject.mint_identifier(ARK_SHOULDER)
+ end
+ end
end
- describe "creating an identifier", :vcr do
- # TODO
+
+ describe "#create_identifier" do
+ let(:id) { "ark:/99999/fk4fn19h88" }
+ let(:http_response) { double(body: "success: ark:/99999/fk4fn19h88") }
+ let(:stub_response) { Response.new(http_response) }
+ before do
+ allow(Request).to receive(:execute) { stub_response }
+ end
+ subject { described_class.new.create_identifier(id) }
+ it "should be a success" do
+ expect(subject).to be_success
+ expect(subject.id).to eq(id)
+ end
end
- describe "minting an identifier", :vcr do
+
+ describe "#mint_identifier" do
+ before { allow(Request).to receive(:execute) { stub_response } }
describe "which is an ARK" do
- it "should be a success" do
- response = subject.mint_identifier(ARK_SHOULDER)
- expect(response).to be_success
- expect(response.message).to match(/#{ARK_SHOULDER}/)
+ let(:stub_response) { Response.new(double(body: "success: ark:/99999/fk4fn19h88")) }
+ subject { described_class.new.mint_identifier(ARK_SHOULDER) }
+ it "should be a succes" do
+ expect(subject).to be_success
+ expect(subject.id).to eq("ark:/99999/fk4fn19h88")
end
end
describe "which is a DOI" do
- it "should be a sucess" do
- response = subject.mint_identifier(DOI_SHOULDER, doi_metadata)
- expect(response).to be_success
- expect(response.message).to match(/#{DOI_SHOULDER}/)
- expect(response.message).to match(/\| ark:/)
+ let(:http_response) { double(body: "success: doi:10.5072/FK2TEST | ark:/99999/fk4fn19h88") }
+ let(:stub_response) { Response.new(http_response) }
+ let(:metadata) do
+ <<-EOS
+datacite.title: Test
+datacite.creator: Duke
+datacite.publisher: Duke
+datacite.publicationyear: 2014
+datacite.resourcetype: Other
+EOS
end
+ subject { described_class.new.mint_identifier(DOI_SHOULDER, metadata) }
+ it "should be a sucess" do
+ expect(subject).to be_success
+ expect(subject.id).to eq("doi:10.5072/FK2TEST")
+ expect(subject.shadow_ark).to eq("ark:/99999/fk4fn19h88")
+ end
end
end
- describe "getting identifier metadata" do
+
+ describe "#get_identifier_metadata" do
+ let(:stub_response) do
+ Response.new(double(body: <<-EOS
+success: ark:/99999/fk4fn19h88
+_updated: 1416507086
+_target: http://ezid.cdlib.org/id/ark:/99999/fk4fn19h88
+_profile: erc
+_ownergroup: apitest
+_owner: apitest
+_export: yes
+_created: 1416507086
+_status: public
+EOS
+ ))
+ end
before do
- @identifier = subject.mint_identifier(ARK_SHOULDER).identifier
+ allow(Request).to receive(:execute) { stub_response }
end
- it "should return the metadata" do
- response = subject.get_identifier_metadata(@identifier)
- expect(response.body).to match(/_status: public/)
+ subject { described_class.new.get_identifier_metadata("ark:/99999/fk4fn19h88") }
+ it "should retrieve the metadata" do
+ expect(subject.metadata).to eq <<-EOS
+_updated: 1416507086
+_target: http://ezid.cdlib.org/id/ark:/99999/fk4fn19h88
+_profile: erc
+_ownergroup: apitest
+_owner: apitest
+_export: yes
+_created: 1416507086
+_status: public
+EOS
end
end
- describe "modifying an identifier" do
+
+ describe "#modify_identifier", type: :feature do
before do
- @identifier = subject.mint_identifier(ARK_SHOULDER).identifier
+ @id = described_class.new.mint_identifier(ARK_SHOULDER).id
end
+ subject { described_class.new.modify_identifier(@id, "dc.title" => "Test") }
it "should update the metadata" do
- subject.modify_identifier(@identifier, "dc.title" => "Test")
- response = subject.get_identifier_metadata(@identifier)
- expect(response.body).to match(/dc.title: Test/)
+ expect(subject).to be_success
+ response = described_class.new.get_identifier_metadata(@id)
+ expect(response.metadata).to match(/dc.title: Test/)
end
end
- describe "deleting an identifier" do
+
+ describe "#delete_identifier", type: :feature do
before do
- @identifier = subject.mint_identifier(ARK_SHOULDER, "_status" => "reserved").identifier
+ @id = described_class.new.mint_identifier(ARK_SHOULDER, "_status" => "reserved").id
end
+ subject { described_class.new.delete_identifier(@id) }
it "should delete the identifier" do
- response = subject.delete_identifier(@identifier)
- expect(response).to be_success
- expect { subject.get_identifier_metadata(@identifier) }.to raise_error
+ expect(subject).to be_success
+ expect { described_class.new.get_identifier_metadata(@id) }.to raise_error
end
end
- describe "server status", :vcr do
+
+ describe "server status" do
+ let(:http_response) do
+ double(body: <<-EOS
+success: EZID is up
+noid: up
+ldap: up
+EOS
+ )
+ end
+ let(:stub_response) { Response.new(http_response) }
+ before do
+ allow(Request).to receive(:execute) { stub_response }
+ end
+ subject { described_class.new.server_status("*") }
it "should report the status of EZID and subsystems" do
- response = subject.server_status("*")
- expect(response).to be_success
- expect(response.message).to eq("EZID is up")
+ expect(subject).to be_success
+ expect(subject).to be_up
+ expect(subject.message).to eq("EZID is up")
+ expect(subject.noid).to eq("up")
+ expect(subject.ldap).to eq("up")
+ expect(subject.datacite).to eq("not checked")
end
end
+
+ describe "error handling" do
+ let(:stub_response) { Response.new(body: "error: bad request - no such identifier") }
+ before do
+ allow(Request).to receive(:execute) { stub_response }
+ end
+ it "should raise an exception" do
+ expect { subject.get_identifier_metadata("invalid") }.to raise_error
+ end
+ end
end
end
-