Commit add5ff89 authored by Mauricio Giacomini Girardello's avatar Mauricio Giacomini Girardello
Browse files

adding client class and authentication resource tests

parent 50bec2c7
......@@ -5,6 +5,7 @@ require "resource_kit"
require_relative "dspace/version"
require_relative "dspace/resources/community_resource"
require_relative "dspace/resources/collection_resource"
require_relative "dspace/resources/authentication_resource"
require_relative "dspace/resources/item_resource"
require_relative "dspace/builders/hash_builder"
require_relative "dspace/builders/model_builder"
......@@ -14,6 +15,7 @@ require_relative "dspace/item"
require_relative "dspace/metadata"
require_relative "dspace/bitstream"
require_relative "dspace/policy"
require_relative "dspace/client"
module Dspace
......
module Dspace
class Client
DSPACE_API = 'https://demo.dspace.org'
attr_reader :access_token
def initialize(options = {})
@access_token = options.with_indifferent_access[:access_token]
@dspace_api = options.with_indifferent_access[:dspace_api]
end
def connection
Faraday.new(connection_options) do |req|
req.adapter :net_http
end
end
def self.resources
{
items: ItemResource,
collections: CollectionResource,
communities: CommunityResource,
status: StatusResource,
authentication: AuthenticationResource
}
end
def method_missing(name, *args, &block)
if self.class.resources.keys.include?(name)
resource(name)
else
super
end
end
def resources
@resources ||= {}
end
def is_running?
resource(:status).test
end
def login(email, password)
@access_token = resource(:authentication).login(email, password)
end
def logout
resource(:authentication).logout
@access_token = nil
end
private
def resource(name)
if self.class.resources.keys.include?(name)
resources[name] ||= self.class.resources[name].new(connection: connection)
resources[name]
end
end
def connection_options
{
url: @dspace_api || DSPACE_API,
headers: {
content_type: 'application/json',
'rest-dspace-token' => access_token.to_s
}
}
end
end
end
\ No newline at end of file
require 'spec_helper'
RSpec.describe Dspace::Resources::AuthenticationResource, resource_kit: true do
subject(:resource) { Dspace::Resources::AuthenticationResource }
context 'login' do
it 'raise an exception when credentials are wrong' do
expect(resource).to have_action(:login).that_handles(403).at_path('/rest/login').with_verb(:post) do |handled|
expect(handled).to raise_error(Dspace::InvalidCredentialsError)
end
end
it 'returns an access token' do
expect(resource).to have_action(:login).that_handles(200, 201).at_path('/rest/login').with_verb(:post) do |handled|
expect(handled).to be_kind_of(String)
end
end
end
context 'logout' do
it 'raise an exception when access token is invalid' do
expect(resource).to have_action(:logout).that_handles(400).at_path('/rest/logout').with_verb(:post) do |handled|
expect(handled).to raise_error(Dspace::InvalidTokenError)
end
end
it 'performs the action' do
expect(resource).to have_action(:logout).that_handles(200, 201, 203, 204).at_path('/rest/logout').with_verb(:post) do |handled|
expect(handled).to eq(true)
end
end
end
end
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment