From 98ba19b5f2c08a1c0a60be5f4137feeaa0b81f88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Wed, 8 Aug 2018 19:05:13 +0200 Subject: [PATCH] [QA] Improve the fork scenario to take a username and password instead of always creating a new user MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes https://gitlab.com/gitlab-org/quality/staging/issues/2. Signed-off-by: Rémy Coutable --- qa/qa/factory/resource/fork.rb | 7 ++- qa/qa/factory/resource/user.rb | 48 ++++++++++++++----- qa/qa/git/repository.rb | 2 +- qa/qa/page/main/login.rb | 25 +++++++--- qa/qa/page/main/sign_up.rb | 12 ++--- qa/qa/runtime/env.rb | 12 +++++ qa/qa/runtime/user.rb | 6 +-- qa/qa/specs/features/api/basics_spec.rb | 2 +- qa/qa/specs/features/api/users_spec.rb | 4 +- .../features/project/fork_project_spec.rb | 10 ++-- qa/spec/runtime/env_spec.rb | 25 ++++++++++ 11 files changed, 114 insertions(+), 39 deletions(-) diff --git a/qa/qa/factory/resource/fork.rb b/qa/qa/factory/resource/fork.rb index 1d0c76a3d30..01969c31438 100644 --- a/qa/qa/factory/resource/fork.rb +++ b/qa/qa/factory/resource/fork.rb @@ -4,7 +4,12 @@ module QA class Fork < Factory::Base dependency Factory::Repository::ProjectPush, as: :push - dependency Factory::Resource::User, as: :user + dependency Factory::Resource::User, as: :user do |user| + if Runtime::Env.forker? + user.username = Runtime::Env.forker_username + user.password = Runtime::Env.forker_password + end + end product(:user) { |factory| factory.user } diff --git a/qa/qa/factory/resource/user.rb b/qa/qa/factory/resource/user.rb index e08df9e0cd0..eac2a873bd5 100644 --- a/qa/qa/factory/resource/user.rb +++ b/qa/qa/factory/resource/user.rb @@ -4,28 +4,52 @@ module QA module Factory module Resource class User < Factory::Base - attr_accessor :name, :username, :email, :password + attr_reader :unique_id + attr_writer :username, :password, :name, :email def initialize - @name = "name-#{SecureRandom.hex(8)}" - @username = "username-#{SecureRandom.hex(8)}" - @email = "mail#{SecureRandom.hex(8)}@mail.com" - @password = 'password' + @unique_id = SecureRandom.hex(8) + end + + def username + @username ||= "qa-user-#{unique_id}" + end + + def password + @password ||= 'password' + end + + def name + @name ||= username + end + + def email + @email ||= "#{username}@example.com" + end + + def credentials_given? + defined?(@username) && defined?(@password) end product(:name) { |factory| factory.name } - product(:username) { |factory| factory.username } - product(:email) { |factory| factory.email } - product(:password) { |factory| factory.password } def fabricate! - Page::Menu::Main.act { sign_out } - Page::Main::Login.act { switch_to_register_tab } - Page::Main::SignUp.perform do |page| - page.sign_up!(name: name, username: username, email: email, password: password) + Page::Menu::Main.perform { |main| main.sign_out } + + if credentials_given? + Page::Main::Login.perform do |login| + login.sign_in_using_credentials(self) + end + else + Page::Main::Login.perform do |login| + login.switch_to_register_tab + end + Page::Main::SignUp.perform do |signup| + signup.sign_up!(self) + end end end end diff --git a/qa/qa/git/repository.rb b/qa/qa/git/repository.rb index 3df6db05970..bdbb18b5045 100644 --- a/qa/qa/git/repository.rb +++ b/qa/qa/git/repository.rb @@ -28,7 +28,7 @@ module QA end def use_default_credentials - self.username = Runtime::User.name + self.username = Runtime::User.username self.password = Runtime::User.password end diff --git a/qa/qa/page/main/login.rb b/qa/qa/page/main/login.rb index 6cdfbd1c125..afc8b66d878 100644 --- a/qa/qa/page/main/login.rb +++ b/qa/qa/page/main/login.rb @@ -40,17 +40,19 @@ module QA end end - def sign_in_using_credentials + def sign_in_using_credentials(user = nil) # Don't try to log-in if we're already logged-in return if Page::Menu::Main.act { has_personal_area?(wait: 0) } using_wait_time 0 do set_initial_password_if_present + raise NotImplementedError if Runtime::User.ldap_user? && user&.credentials_given? + if Runtime::User.ldap_user? sign_in_using_ldap_credentials else - sign_in_using_gitlab_credentials + sign_in_using_gitlab_credentials(user || Runtime::User) end end @@ -69,21 +71,30 @@ module QA click_on 'Register' end + def switch_to_ldap_tab + click_on 'LDAP' + end + + def switch_to_standard_tab + click_on 'Standard' + end + private def sign_in_using_ldap_credentials - click_link 'LDAP' + switch_to_ldap_tab fill_in :username, with: Runtime::User.ldap_username fill_in :password, with: Runtime::User.ldap_password click_button 'Sign in' end - def sign_in_using_gitlab_credentials - click_link 'Standard' if page.has_content?('LDAP') + def sign_in_using_gitlab_credentials(user) + switch_to_sign_in_tab unless page.has_button?('Sign in') + switch_to_standard_tab if page.has_content?('LDAP') - fill_in :user_login, with: Runtime::User.name - fill_in :user_password, with: Runtime::User.password + fill_in :user_login, with: user.username + fill_in :user_password, with: user.password click_button 'Sign in' end diff --git a/qa/qa/page/main/sign_up.rb b/qa/qa/page/main/sign_up.rb index 9a834e94b81..33ab56236f4 100644 --- a/qa/qa/page/main/sign_up.rb +++ b/qa/qa/page/main/sign_up.rb @@ -11,12 +11,12 @@ module QA element :register_button, 'submit "Register"' end - def sign_up!(name:, username:, email:, password:) - fill_in :new_user_name, with: name - fill_in :new_user_username, with: username - fill_in :new_user_email, with: email - fill_in :new_user_email_confirmation, with: email - fill_in :new_user_password, with: password + def sign_up!(user) + fill_in :new_user_name, with: user.name + fill_in :new_user_username, with: user.username + fill_in :new_user_email, with: user.email + fill_in :new_user_email_confirmation, with: user.email + fill_in :new_user_password, with: user.password click_button 'Register' Page::Menu::Main.act { has_personal_area? } diff --git a/qa/qa/runtime/env.rb b/qa/qa/runtime/env.rb index 5dc194e0aef..841c959045f 100644 --- a/qa/qa/runtime/env.rb +++ b/qa/qa/runtime/env.rb @@ -39,6 +39,18 @@ module QA ENV['GITLAB_PASSWORD'] end + def forker? + forker_username && forker_password + end + + def forker_username + ENV['GITLAB_FORKER_USERNAME'] + end + + def forker_password + ENV['GITLAB_FORKER_PASSWORD'] + end + def ldap_username ENV['GITLAB_LDAP_USERNAME'] end diff --git a/qa/qa/runtime/user.rb b/qa/qa/runtime/user.rb index c80ee6d4d96..b016777c987 100644 --- a/qa/qa/runtime/user.rb +++ b/qa/qa/runtime/user.rb @@ -3,12 +3,12 @@ module QA module User extend self - def default_name + def default_username 'root' end - def name - Runtime::Env.user_username || default_name + def username + Runtime::Env.user_username || default_username end def password diff --git a/qa/qa/specs/features/api/basics_spec.rb b/qa/qa/specs/features/api/basics_spec.rb index 6563b56d1b4..8234b6e0099 100644 --- a/qa/qa/specs/features/api/basics_spec.rb +++ b/qa/qa/specs/features/api/basics_spec.rb @@ -7,7 +7,7 @@ module QA end let(:project_name) { "api-basics-#{SecureRandom.hex(8)}" } - let(:sanitized_project_path) { CGI.escape("#{Runtime::User.name}/#{project_name}") } + let(:sanitized_project_path) { CGI.escape("#{Runtime::User.username}/#{project_name}") } it 'user creates a project with a file and deletes them afterwards' do create_project_request = Runtime::API::Request.new(@api_client, '/projects') diff --git a/qa/qa/specs/features/api/users_spec.rb b/qa/qa/specs/features/api/users_spec.rb index 8a63d8095c9..5edc72b7003 100644 --- a/qa/qa/specs/features/api/users_spec.rb +++ b/qa/qa/specs/features/api/users_spec.rb @@ -14,11 +14,11 @@ module QA end it 'submit request with a valid user name' do - get request.url, { params: { username: Runtime::User.name } } + get request.url, { params: { username: Runtime::User.username } } expect_status(200) expect(json_body).to contain_exactly( - a_hash_including(username: Runtime::User.name) + a_hash_including(username: Runtime::User.username) ) end diff --git a/qa/qa/specs/features/project/fork_project_spec.rb b/qa/qa/specs/features/project/fork_project_spec.rb index 8ad0120305a..38786a6550a 100644 --- a/qa/qa/specs/features/project/fork_project_spec.rb +++ b/qa/qa/specs/features/project/fork_project_spec.rb @@ -8,14 +8,12 @@ module QA merge_request.fork_branch = 'feature-branch' end - Page::Menu::Main.act { sign_out } - Page::Main::Login.act do - switch_to_sign_in_tab - sign_in_using_credentials - end + Page::Menu::Main.perform { |main| main.sign_out } + Page::Main::Login.perform { |login| login.sign_in_using_credentials } merge_request.visit! - Page::MergeRequest::Show.act { merge! } + + Page::MergeRequest::Show.perform { |show| show.merge! } expect(page).to have_content('The changes were merged') end diff --git a/qa/spec/runtime/env_spec.rb b/qa/spec/runtime/env_spec.rb index 851026c71f0..ccc0b906845 100644 --- a/qa/spec/runtime/env_spec.rb +++ b/qa/spec/runtime/env_spec.rb @@ -77,6 +77,31 @@ describe QA::Runtime::Env do end end + describe '.forker?' do + it 'returns false if no forker credentials are defined' do + expect(described_class).not_to be_forker + end + + it 'returns false if only forker username is defined' do + stub_env('GITLAB_FORKER_USERNAME', 'foo') + + expect(described_class).not_to be_forker + end + + it 'returns false if only forker password is defined' do + stub_env('GITLAB_FORKER_PASSWORD', 'bar') + + expect(described_class).not_to be_forker + end + + it 'returns true if forker username and password are defined' do + stub_env('GITLAB_FORKER_USERNAME', 'foo') + stub_env('GITLAB_FORKER_PASSWORD', 'bar') + + expect(described_class).to be_forker + end + end + describe '.github_access_token' do it 'returns "" if GITHUB_ACCESS_TOKEN is not defined' do expect(described_class.github_access_token).to eq('')