summaryrefslogtreecommitdiff
path: root/qa/qa/runtime
diff options
context:
space:
mode:
authorBrett Walker <brett@digitalmoksha.com>2018-01-23 10:23:23 +0000
committerGrzegorz Bizon <grzegorz@gitlab.com>2018-01-23 10:23:23 +0000
commitb8b9e9eb8e3f285bc3040f3dc425deeada585aae (patch)
tree87934fbe6b2b2b66be25888152bcbcd4af82e970 /qa/qa/runtime
parentfcfda3ef82992e5f56573dffb56b2a5896c24aff (diff)
downloadgitlab-ce-b8b9e9eb8e3f285bc3040f3dc425deeada585aae.tar.gz
Add ability to drive the API in QA specs
Diffstat (limited to 'qa/qa/runtime')
-rw-r--r--qa/qa/runtime/address.rb20
-rw-r--r--qa/qa/runtime/api.rb82
-rw-r--r--qa/qa/runtime/browser.rb19
-rw-r--r--qa/qa/runtime/env.rb6
4 files changed, 114 insertions, 13 deletions
diff --git a/qa/qa/runtime/address.rb b/qa/qa/runtime/address.rb
new file mode 100644
index 00000000000..ffad3974b02
--- /dev/null
+++ b/qa/qa/runtime/address.rb
@@ -0,0 +1,20 @@
+module QA
+ module Runtime
+ class Address
+ attr_reader :address
+
+ def initialize(instance, page = nil)
+ @instance = instance
+ @address = host + (page.is_a?(String) ? page : page&.path)
+ end
+
+ def host
+ if @instance.is_a?(Symbol)
+ Runtime::Scenario.send("#{@instance}_address")
+ else
+ @instance.to_s
+ end
+ end
+ end
+ end
+end
diff --git a/qa/qa/runtime/api.rb b/qa/qa/runtime/api.rb
new file mode 100644
index 00000000000..e2a096b971d
--- /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
+
+ private
+
+ def create_personal_access_token
+ Runtime::Browser.visit(@address, Page::Main::Login) do
+ Page::Main::Login.act { sign_in_using_credentials }
+ Factory::Resource::PersonalAccessToken.fabricate!.access_token
+ end
+ 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_address = Runtime::Address.new(api_client.address, request_path)
+ end
+
+ def url
+ @session_address.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 = File.join('/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.include?('?') ? '&' : '?')
+ full_path << query_string
+ end
+
+ full_path
+ end
+ end
+ end
+ end
+end
diff --git a/qa/qa/runtime/browser.rb b/qa/qa/runtime/browser.rb
index 14b2a488760..7b1be3d5ef3 100644
--- a/qa/qa/runtime/browser.rb
+++ b/qa/qa/runtime/browser.rb
@@ -24,9 +24,7 @@ module QA
# based on `Runtime::Scenario#something_address`.
#
def visit(address, page, &block)
- Browser::Session.new(address, page).tap do |session|
- session.perform(&block)
- end
+ Browser::Session.new(address, page).perform(&block)
end
def self.visit(address, page, &block)
@@ -94,20 +92,15 @@ module QA
include Capybara::DSL
def initialize(instance, page = nil)
- @instance = instance
- @address = host + page&.path
+ @session_address = Runtime::Address.new(instance, page)
end
- def host
- if @instance.is_a?(Symbol)
- Runtime::Scenario.send("#{@instance}_address")
- else
- @instance.to_s
- end
+ def url
+ @session_address.address
end
def perform(&block)
- visit(@address)
+ visit(url)
yield if block_given?
rescue
@@ -130,7 +123,7 @@ module QA
# See gitlab-org/gitlab-qa#102
#
def clear!
- visit(@address)
+ visit(url)
reset_session!
end
end
diff --git a/qa/qa/runtime/env.rb b/qa/qa/runtime/env.rb
index d5c28e9a7db..56944e8b641 100644
--- a/qa/qa/runtime/env.rb
+++ b/qa/qa/runtime/env.rb
@@ -3,6 +3,7 @@ module QA
module Env
extend self
+ # set to 'false' to have Chrome run visibly instead of headless
def chrome_headless?
(ENV['CHROME_HEADLESS'] =~ /^(false|no|0)$/i) != 0
end
@@ -10,6 +11,11 @@ module QA
def running_in_ci?
ENV['CI'] || ENV['CI_SERVER']
end
+
+ # specifies token that can be used for the api
+ def personal_access_token
+ ENV['PERSONAL_ACCESS_TOKEN']
+ end
end
end
end