diff options
author | Brett Walker <bwalker@gitlab.com> | 2018-01-06 13:43:32 +0100 |
---|---|---|
committer | Brett Walker <bwalker@gitlab.com> | 2018-01-22 17:25:10 +0100 |
commit | 590956d0d14678a3498c077737aaa2508f267520 (patch) | |
tree | 0eb7e72a4bcb0baf5b90a260f9f7bbee67fcc5a0 | |
parent | 2bd8eda4acba634f6a6ff44eadfe3578741f2a1c (diff) | |
download | gitlab-ce-590956d0d14678a3498c077737aaa2508f267520.tar.gz |
add support for driving the api in QA specs
-rw-r--r-- | qa/Gemfile | 1 | ||||
-rw-r--r-- | qa/Gemfile.lock | 6 | ||||
-rw-r--r-- | qa/qa.rb | 20 | ||||
-rw-r--r-- | qa/qa/factory/product.rb | 1 | ||||
-rw-r--r-- | qa/qa/factory/resource/personal_access_token.rb | 36 | ||||
-rw-r--r-- | qa/qa/page/menu/main.rb | 8 | ||||
-rw-r--r-- | qa/qa/page/menu/user_settings.rb | 21 | ||||
-rw-r--r-- | qa/qa/page/user/settings/access_tokens.rb | 25 | ||||
-rw-r--r-- | qa/qa/runtime/api.rb | 40 | ||||
-rw-r--r-- | qa/qa/runtime/env.rb | 6 | ||||
-rw-r--r-- | qa/qa/specs/runner.rb | 1 | ||||
-rw-r--r-- | qa/qa/support/api_helpers.rb | 40 | ||||
-rw-r--r-- | qa/qa/support/matchers/have_gitlab_api_status.rb | 17 |
13 files changed, 220 insertions, 2 deletions
diff --git a/qa/Gemfile b/qa/Gemfile index 4c866a3f893..efb4f87d83b 100644 --- a/qa/Gemfile +++ b/qa/Gemfile @@ -6,3 +6,4 @@ gem 'capybara-screenshot', '~> 1.0.18' gem 'rake', '~> 12.3.0' gem 'rspec', '~> 3.7' gem 'selenium-webdriver', '~> 3.8.0' +gem 'faraday', '~> 0.12' diff --git a/qa/Gemfile.lock b/qa/Gemfile.lock index 88d5fe834a0..85dd648f835 100644 --- a/qa/Gemfile.lock +++ b/qa/Gemfile.lock @@ -18,12 +18,15 @@ GEM ffi (~> 1.0, >= 1.0.11) coderay (1.1.2) diff-lcs (1.3) + faraday (0.13.1) + multipart-post (>= 1.2, < 3) ffi (1.9.18) launchy (2.4.3) addressable (~> 2.3) method_source (0.9.0) mini_mime (1.0.0) mini_portile2 (2.3.0) + multipart-post (2.0.0) nokogiri (1.8.1) mini_portile2 (~> 2.3.0) pry (0.11.3) @@ -63,10 +66,11 @@ PLATFORMS DEPENDENCIES capybara (~> 2.16.1) capybara-screenshot (~> 1.0.18) + faraday (~> 0.12) pry-byebug (~> 3.5.1) rake (~> 12.3.0) rspec (~> 3.7) selenium-webdriver (~> 3.8.0) BUNDLED WITH - 1.16.0 + 1.16.1 @@ -11,6 +11,7 @@ module QA autoload :Scenario, 'qa/runtime/scenario' autoload :Browser, 'qa/runtime/browser' autoload :Env, 'qa/runtime/env' + autoload :API, 'qa/runtime/api' end ## @@ -26,6 +27,7 @@ module QA autoload :Group, 'qa/factory/resource/group' autoload :Project, 'qa/factory/resource/project' autoload :DeployKey, 'qa/factory/resource/deploy_key' + autoload :PersonalAccessToken, 'qa/factory/resource/personal_access_token' end module Repository @@ -85,6 +87,7 @@ module QA autoload :Main, 'qa/page/menu/main' autoload :Side, 'qa/page/menu/side' autoload :Admin, 'qa/page/menu/admin' + autoload :UserSettings, 'qa/page/menu/user_settings' end module Dashboard @@ -108,6 +111,12 @@ module QA end end + module User + module Settings + autoload :AccessTokens, 'qa/page/user/settings/access_tokens' + end + end + module Admin autoload :Settings, 'qa/page/admin/settings' end @@ -139,6 +148,17 @@ 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' + + # module Matchers + # autoload :ApiHelpers, 'qa/support/matchers/have_gitlab_http_status' + # end + end end QA::Runtime::Release.extend_autoloads! diff --git a/qa/qa/factory/product.rb b/qa/qa/factory/product.rb index d004e642f9b..937bae7ac11 100644 --- a/qa/qa/factory/product.rb +++ b/qa/qa/factory/product.rb @@ -4,6 +4,7 @@ module QA module Factory class Product include Capybara::DSL + attr_reader :factory Attribute = Struct.new(:name, :block) diff --git a/qa/qa/factory/resource/personal_access_token.rb b/qa/qa/factory/resource/personal_access_token.rb new file mode 100644 index 00000000000..ab4f938be37 --- /dev/null +++ b/qa/qa/factory/resource/personal_access_token.rb @@ -0,0 +1,36 @@ +# 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 +module QA + module Factory + module Resource + class PersonalAccessToken < Factory::Base + attr_accessor :name, :access_token + + def fabricate!(sign_in = true) + @access_token = Runtime::Env.personal_access_token + unless @access_token + if sign_in + Runtime::Browser.visit(:gitlab, Page::Main::Login) + Page::Main::Login.act { sign_in_using_credentials } + end + + Page::Menu::Main.act { go_to_profile_settings } + Page::Menu::UserSettings.act { click_access_tokens } + + Page::User::Settings::AccessTokens.perform do |page| + page.fill_token_name(name || 'api-test-token') + page.check_api + page.create_token + @access_token = page.created_access_token + end + + if sign_in + Page::Menu::Main.act { sign_out } + end + end + end + end + end + end +end diff --git a/qa/qa/page/menu/main.rb b/qa/qa/page/menu/main.rb index f8978b8a5f7..f8184dcb9e1 100644 --- a/qa/qa/page/menu/main.rb +++ b/qa/qa/page/menu/main.rb @@ -40,7 +40,13 @@ module QA def sign_out within_user_menu do - click_link('Sign out') + click_link 'Sign out' + end + end + + def go_to_profile_settings + within_user_menu do + click_link 'Settings' end end diff --git a/qa/qa/page/menu/user_settings.rb b/qa/qa/page/menu/user_settings.rb new file mode 100644 index 00000000000..00d73ae6919 --- /dev/null +++ b/qa/qa/page/menu/user_settings.rb @@ -0,0 +1,21 @@ +module QA + module Page + module Menu + class UserSettings < Page::Base + def click_access_tokens + within_sidebar do + click_link('Access Tokens') + end + end + + private + + def within_sidebar + page.within('.sidebar-top-level-items') do + yield + end + end + end + end + end +end diff --git a/qa/qa/page/user/settings/access_tokens.rb b/qa/qa/page/user/settings/access_tokens.rb new file mode 100644 index 00000000000..3171a47b678 --- /dev/null +++ b/qa/qa/page/user/settings/access_tokens.rb @@ -0,0 +1,25 @@ +module QA + module Page + module User + module Settings + class AccessTokens < Page::Base + def fill_token_name(name) + fill_in 'personal_access_token_name', with: name + end + + def check_api + check 'personal_access_token_scopes_api' + end + + def create_token + click_on 'Create personal access token' + end + + def created_access_token + page.find('#created-personal-access-token').value + end + 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..03b17354982 --- /dev/null +++ b/qa/qa/runtime/api.rb @@ -0,0 +1,40 @@ +require 'faraday' + +module QA + module Runtime + class API + ## + # GET an endpoint that belongs to a GitLab instance under a given address + # + # Example: + # + # get(:gitlab, api('/users', personal_access_token: 'something')) + # 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) + Faraday.get API::Session.new(address, page).address + end + + def self.version + 'v4' + end + + class Session + 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 + + def address + @address + end + end + end + 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 diff --git a/qa/qa/specs/runner.rb b/qa/qa/specs/runner.rb index 3f7b75df986..b61ae43f980 100644 --- a/qa/qa/specs/runner.rb +++ b/qa/qa/specs/runner.rb @@ -1,4 +1,5 @@ require 'rspec/core' +require_relative '../support/matchers/have_gitlab_api_status' module QA module Specs diff --git a/qa/qa/support/api_helpers.rb b/qa/qa/support/api_helpers.rb new file mode 100644 index 00000000000..092f16afabe --- /dev/null +++ b/qa/qa/support/api_helpers.rb @@ -0,0 +1,40 @@ +module QA + module Support + module ApiHelpers + # 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: QA::Runtime::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 diff --git a/qa/qa/support/matchers/have_gitlab_api_status.rb b/qa/qa/support/matchers/have_gitlab_api_status.rb new file mode 100644 index 00000000000..cf51bb416c0 --- /dev/null +++ b/qa/qa/support/matchers/have_gitlab_api_status.rb @@ -0,0 +1,17 @@ +# checks a response from an api call using Faraday +RSpec::Matchers.define :have_gitlab_api_status do |expected| + match do |actual| + expect(actual.status).to eq expected + end + + description do + "respond with numeric status code #{expected}" + end + + failure_message do |actual| + response_code = actual.status + + "expected the response to have status code #{expected.inspect}" \ + " but it was #{response_code}. The response was: #{actual.body}" + end +end |