summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrett Walker <bwalker@gitlab.com>2018-01-18 12:22:54 +0100
committerBrett Walker <bwalker@gitlab.com>2018-01-22 17:25:11 +0100
commitd67567995a60470999f46267e5b47400e8f46db0 (patch)
tree09f80fcc7272001a93a46f80d54ad8811461878a
parentd82901338cf390f9c9776010cc75b16457230c8c (diff)
downloadgitlab-ce-d67567995a60470999f46267e5b47400e8f46db0.tar.gz
refactored to create API::Client and API::Request classes
-rw-r--r--qa/qa.rb8
-rw-r--r--qa/qa/factory/resource/personal_access_token.rb17
-rw-r--r--qa/qa/runtime/api.rb82
-rw-r--r--qa/qa/specs/features/api/users_spec.rb16
-rw-r--r--qa/qa/support/api_helpers.rb44
5 files changed, 91 insertions, 76 deletions
diff --git a/qa/qa.rb b/qa/qa.rb
index acd1a327848..40bdcc726ae 100644
--- a/qa/qa.rb
+++ b/qa/qa.rb
@@ -12,6 +12,7 @@ module QA
autoload :Browser, 'qa/runtime/browser'
autoload :Env, 'qa/runtime/env'
autoload :Session, 'qa/runtime/session'
+ autoload :API, 'qa/runtime/api'
end
##
@@ -146,13 +147,6 @@ module QA
autoload :Config, 'qa/specs/config'
autoload :Runner, 'qa/specs/runner'
end
-
- ##
- # Classes that provide support methods
- #
- module Support
- autoload :ApiHelpers, 'qa/support/api_helpers'
- end
end
QA::Runtime::Release.extend_autoloads!
diff --git a/qa/qa/factory/resource/personal_access_token.rb b/qa/qa/factory/resource/personal_access_token.rb
index aebfda6510d..514e3615d18 100644
--- a/qa/qa/factory/resource/personal_access_token.rb
+++ b/qa/qa/factory/resource/personal_access_token.rb
@@ -3,28 +3,15 @@ module QA
module Resource
##
# Create a personal access token that can be used by the api
- # set the environment variable PERSONAL_ACCESS_TOKEN to use a
- # specific access token rather than create one from the UI
#
class PersonalAccessToken < Factory::Base
attr_accessor :name
product :access_token do
- if Runtime::Env.personal_access_token
- Runtime::Env.personal_access_token
- else
- Page::Profile::PersonalAccessTokens.act { created_access_token }
- end
+ Page::Profile::PersonalAccessTokens.act { created_access_token }
end
- def fabricate!(sign_in_address = :gitlab)
- return if Runtime::Env.personal_access_token
-
- if sign_in_address
- Runtime::Browser.visit(sign_in_address, Page::Main::Login)
- Page::Main::Login.act { sign_in_using_credentials }
- end
-
+ def fabricate!
Page::Menu::Main.act { go_to_profile_settings }
Page::Menu::Profile.act { click_access_tokens }
diff --git a/qa/qa/runtime/api.rb b/qa/qa/runtime/api.rb
new file mode 100644
index 00000000000..ee206fe299c
--- /dev/null
+++ b/qa/qa/runtime/api.rb
@@ -0,0 +1,82 @@
+require 'airborne'
+
+module QA
+ module Runtime
+ module API
+ class Client
+ attr_reader :address
+
+ def initialize(address = :gitlab)
+ @address = address
+ end
+
+ def personal_access_token
+ @personal_access_token ||= get_personal_access_token
+ end
+
+ def get_personal_access_token
+ # you can set the environment variable PERSONAL_ACCESS_TOKEN
+ # to use a specific access token rather than create one from the UI
+ if Runtime::Env.personal_access_token
+ Runtime::Env.personal_access_token
+ else
+ create_personal_access_token
+ end
+ end
+
+ def create_personal_access_token
+ Runtime::Browser.visit(@address, Page::Main::Login)
+ Page::Main::Login.act { sign_in_using_credentials }
+ token = Factory::Resource::PersonalAccessToken.fabricate!.access_token
+ Page::Menu::Main.act { sign_out }
+
+ token
+ end
+ end
+
+ class Request
+ API_VERSION = 'v4'.freeze
+
+ def initialize(api_client, path, personal_access_token: nil)
+ personal_access_token ||= api_client.personal_access_token
+ request_path = request_path(path, personal_access_token: personal_access_token)
+ @session = Runtime::Session.new(api_client.address, request_path)
+ end
+
+ def url
+ @session.address
+ end
+
+ # Prepend a request path with the path to the API
+ #
+ # path - Path to append
+ #
+ # Examples
+ #
+ # >> request_path('/issues')
+ # => "/api/v4/issues"
+ #
+ # >> request_path('/issues', personal_access_token: 'sometoken)
+ # => "/api/v4/issues?private_token=..."
+ #
+ # Returns the relative path to the requested API resource
+ def request_path(path, version: API_VERSION, personal_access_token: nil, oauth_access_token: nil)
+ full_path = "/api/#{version}#{path}"
+
+ if oauth_access_token
+ query_string = "access_token=#{oauth_access_token}"
+ elsif personal_access_token
+ query_string = "private_token=#{personal_access_token}"
+ end
+
+ if query_string
+ full_path << (path.index('?') ? '&' : '?')
+ full_path << query_string
+ end
+
+ full_path
+ end
+ end
+ end
+ end
+end
diff --git a/qa/qa/specs/features/api/users_spec.rb b/qa/qa/specs/features/api/users_spec.rb
index a7f9d559558..056787845ab 100644
--- a/qa/qa/specs/features/api/users_spec.rb
+++ b/qa/qa/specs/features/api/users_spec.rb
@@ -1,23 +1,19 @@
module QA
feature 'API users', :core do
- include Support::ApiHelpers
-
before(:context) do
- Runtime::Browser.visit(:gitlab, Page::Main::Login)
- Page::Main::Login.act { sign_in_using_credentials }
- @access_token = Factory::Resource::PersonalAccessToken.fabricate!.access_token
+ @api_client = Runtime::API::Client.new(:gitlab)
end
context 'when authenticated' do
- let(:session) { Runtime::Session.new(:gitlab, api('/users', personal_access_token: @access_token)) }
+ let(:request) { Runtime::API::Request.new(@api_client, '/users') }
scenario 'get list of users' do
- get session.address
+ get request.url
expect_status(200)
end
scenario 'submit request with an invalid user name' do
- get session.address, { params: { username: 'invalid' } }
+ get request.url, { params: { username: 'invalid' } }
expect_status(200)
expect(json_body).to be_an Array
expect(json_body.size).to eq(0)
@@ -25,8 +21,8 @@ module QA
end
scenario 'submit request with an invalid token' do
- session = Runtime::Session.new(:gitlab, api('/users', personal_access_token: 'invalid'))
- get session.address
+ request = Runtime::API::Request.new(@api_client, '/users', personal_access_token: 'invalid')
+ get request.url
expect_status(401)
end
diff --git a/qa/qa/support/api_helpers.rb b/qa/qa/support/api_helpers.rb
deleted file mode 100644
index ccabf7fe809..00000000000
--- a/qa/qa/support/api_helpers.rb
+++ /dev/null
@@ -1,44 +0,0 @@
-require 'airborne'
-
-module QA
- module Support
- module ApiHelpers
- API_VERSION = 'v4'.freeze
-
- # Public: Prepend a request path with the path to the API
- #
- # path - Path to append
- # user - User object - If provided, automatically appends private_token query
- # string for authenticated requests
- #
- # Examples
- #
- # >> api('/issues')
- # => "/api/v2/issues"
- #
- # >> api('/issues', User.last)
- # => "/api/v2/issues?private_token=..."
- #
- # >> api('/issues?foo=bar', User.last)
- # => "/api/v2/issues?foo=bar&private_token=..."
- #
- # Returns the relative path to the requested API resource
- def api(path, version: API_VERSION, personal_access_token: nil, oauth_access_token: nil)
- full_path = "/api/#{version}#{path}"
-
- if oauth_access_token
- query_string = "access_token=#{oauth_access_token}"
- elsif personal_access_token
- query_string = "private_token=#{personal_access_token}"
- end
-
- if query_string
- full_path << (path.index('?') ? '&' : '?')
- full_path << query_string
- end
-
- full_path
- end
- end
- end
-end