diff options
-rw-r--r-- | qa/qa/runtime/api.rb | 77 | ||||
-rw-r--r-- | qa/qa/specs/features/api/users_spec.rb | 18 |
2 files changed, 51 insertions, 44 deletions
diff --git a/qa/qa/runtime/api.rb b/qa/qa/runtime/api.rb index d28310ea229..d72c7a2e465 100644 --- a/qa/qa/runtime/api.rb +++ b/qa/qa/runtime/api.rb @@ -1,50 +1,57 @@ -require 'faraday' - module QA module Runtime - class API + module API VERSION = 'v4'.freeze - ## - # GET an endpoint that belongs to a GitLab instance under a given address - # - # Example: - # - # get(:gitlab, api('/users', personal_access_token: 'something')) - # get(:gitlab, api('/users', personal_access_token: 'something'), username: value, ...) - # get('http://gitlab.example', '/api/v4/users?private_token=something') - # - # In case of an address that is a symbol we will try to guess address - # based on `Runtime::Scenario#something_address`. - def self.get(address, page, *args) - page = self.add_query_values(page, args) - response = Faraday.get(API::Session.new(address, page).address) - json = response.status == 200 ? JSON.parse(response.body) : nil - [response, json] - end + class Client + ## + # GET an endpoint that belongs to a GitLab instance under a given address + # + # Example: + # + # get(:gitlab, api('/users', personal_access_token: 'something')) + # get(:gitlab, api('/users', personal_access_token: 'something'), username: value, ...) + # get('http://gitlab.example', '/api/v4/users?private_token=something') + # + # In case of an address that is a symbol we will try to guess address + # based on `Runtime::Scenario#something_address`. + def self.get(address, page, *args) + page = self.add_query_values(page, args) + API::Response.new(Faraday.get(API::Client::Session.new(address, page).address)) + end - def self.add_query_values(path, args) - if args.any? - query_string = Hash(*args).map { |key, value| "#{key}=#{value}" }.join('&') + def self.add_query_values(path, args) + if args.any? + query_string = Hash(*args).map { |key, value| "#{key}=#{value}" }.join('&') - if query_string - path << (path.index('?') ? '&' : '?') - path << query_string + if query_string + path << (path.index('?') ? '&' : '?') + path << query_string + end end + path end - path - end - class Session - attr_reader :address + class Session + attr_reader :address - def initialize(instance, page = nil) - @instance = instance - @address = host + (page.is_a?(String) ? page : page&.path) + def initialize(instance, page = nil) + @instance = instance + @address = host + (page.is_a?(String) ? page : page&.path) + end + + def host + @instance.is_a?(Symbol) ? Runtime::Scenario.send("#{@instance}_address") : @instance.to_s + end end + end + + class Response + attr_reader :response, :json - def host - @instance.is_a?(Symbol) ? Runtime::Scenario.send("#{@instance}_address") : @instance.to_s + def initialize(response) + @response = response + @json = @response.status == 200 ? JSON.parse(@response.body) : nil end end end diff --git a/qa/qa/specs/features/api/users_spec.rb b/qa/qa/specs/features/api/users_spec.rb index 5577384a28d..80ad0b3bd8e 100644 --- a/qa/qa/specs/features/api/users_spec.rb +++ b/qa/qa/specs/features/api/users_spec.rb @@ -9,24 +9,24 @@ module QA context "when authenticated" do scenario 'get list of users' do - response, _json = Runtime::API.get(:gitlab, api('/users', personal_access_token: @access_token)) - expect(response).to have_gitlab_api_status(200) + response = Runtime::API::Client.get(:gitlab, api('/users', personal_access_token: @access_token)) + expect(response.response).to have_gitlab_api_status(200) end scenario 'returns an empty response when an invalid `username` parameter is passed' do - response, json = Runtime::API.get(:gitlab, api('/users', personal_access_token: @access_token), - username: 'invalid') + response = Runtime::API::Client.get(:gitlab, api('/users', personal_access_token: @access_token), + username: 'invalid') - expect(response).to have_gitlab_api_status(200) - expect(json).to be_an Array - expect(json.size).to eq(0) + expect(response.response).to have_gitlab_api_status(200) + expect(response.json).to be_an Array + expect(response.json.size).to eq(0) end end scenario "returns authorization error when token is invalid" do - response, _json = Runtime::API.get(:gitlab, api('/users', personal_access_token: 'invalid')) + response = Runtime::API::Client.get(:gitlab, api('/users', personal_access_token: 'invalid')) - expect(response).to have_gitlab_api_status(401) + expect(response.response).to have_gitlab_api_status(401) end end end |