Commit 98ba19b5 authored by Rémy Coutable's avatar Rémy Coutable

[QA] Improve the fork scenario to take a username and password instead of...

[QA] Improve the fork scenario to take a username and password instead of always creating a new user

Fixes https://gitlab.com/gitlab-org/quality/staging/issues/2.
Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent 0a630e5e
......@@ -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 }
......
......@@ -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
product(:name) { |factory| factory.name }
def username
@username ||= "qa-user-#{unique_id}"
end
product(:username) { |factory| factory.username }
def password
@password ||= 'password'
end
product(:email) { |factory| factory.email }
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
......
......@@ -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
......
......@@ -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
......
......@@ -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? }
......
......@@ -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
......
......@@ -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
......
......@@ -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')
......
......@@ -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
......
......@@ -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
......
......@@ -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('')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment