From 64500f44d382e075acff83c4ee07465ce6c8bc06 Mon Sep 17 00:00:00 2001
From: Mark Lapierre <mlapierre@gitlab.com>
Date: Tue, 15 Oct 2019 12:47:16 +0000
Subject: [PATCH] Fetch user via API in fabricate_or_use

Previously fabricate_or_use would only create a User instance with the
provided username and password. This was fine when only those creds
were needed (e.g., to sign on), but it's not enough in tests that
need other user details, e.g., the user's name.

This changes fabricate_or_use so that it fetches the user via the API,
which provides access to the user's details.
---
 qa/qa/git/repository.rb                                     | 2 +-
 qa/qa/resource/repository/commit.rb                         | 6 +++---
 qa/qa/resource/user.rb                                      | 5 ++---
 .../api/3_create/repository/project_archive_compare_spec.rb | 2 +-
 .../3_create/repository/add_file_template_spec.rb           | 6 ++++--
 qa/spec/resource/user_spec.rb                               | 4 ++--
 6 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/qa/qa/git/repository.rb b/qa/qa/git/repository.rb
index 09052a5e33f15..f56fab63198a8 100644
--- a/qa/qa/git/repository.rb
+++ b/qa/qa/git/repository.rb
@@ -76,7 +76,7 @@ def shallow_clone
       end
 
       def configure_identity(name, email)
-        run(%Q{git config user.name #{name}})
+        run(%Q{git config user.name "#{name}"})
         run(%Q{git config user.email #{email}})
       end
 
diff --git a/qa/qa/resource/repository/commit.rb b/qa/qa/resource/repository/commit.rb
index 8ce5a137486db..4b5e8535ade7c 100644
--- a/qa/qa/resource/repository/commit.rb
+++ b/qa/qa/resource/repository/commit.rb
@@ -59,9 +59,9 @@ def api_post_body
 
         def actions
           pending_actions = []
-          @add_files.map { |file| pending_actions << file.merge({ action: "create" }) } if @add_files
-          @update_files.map { |file| pending_actions << file.merge({ action: "update" }) } if @update_files
-          pending_actions
+          pending_actions << @add_files.map { |file| file.merge({ action: "create" }) } if @add_files
+          pending_actions << @update_files.map { |file| file.merge({ action: "update" }) } if @update_files
+          pending_actions.flatten
         end
 
         private
diff --git a/qa/qa/resource/user.rb b/qa/qa/resource/user.rb
index c166a4d4b59cd..dcf145c9882fb 100644
--- a/qa/qa/resource/user.rb
+++ b/qa/qa/resource/user.rb
@@ -26,7 +26,7 @@ def password
       end
 
       def name
-        @name ||= api_resource&.dig(:name) || username
+        @name ||= api_resource&.dig(:name) || "QA User #{unique_id}"
       end
 
       def email
@@ -91,9 +91,8 @@ def api_post_body
 
       def self.fabricate_or_use(username = nil, password = nil)
         if Runtime::Env.signup_disabled?
-          self.new.tap do |user|
+          self.fabricate_via_api! do |user|
             user.username = username
-            user.password = password
           end
         else
           self.fabricate!
diff --git a/qa/qa/specs/features/api/3_create/repository/project_archive_compare_spec.rb b/qa/qa/specs/features/api/3_create/repository/project_archive_compare_spec.rb
index ac5db53ad40b0..5ba434a77813c 100644
--- a/qa/qa/specs/features/api/3_create/repository/project_archive_compare_spec.rb
+++ b/qa/qa/specs/features/api/3_create/repository/project_archive_compare_spec.rb
@@ -46,7 +46,7 @@ def create_project(user, api_client, project_name)
           project.standalone = true
           project.add_name_uuid = false
           project.name = project_name
-          project.path_with_namespace = "#{user.name}/#{project_name}"
+          project.path_with_namespace = "#{user.username}/#{project_name}"
           project.user = user
           project.api_client = api_client
         end
diff --git a/qa/qa/specs/features/browser_ui/3_create/repository/add_file_template_spec.rb b/qa/qa/specs/features/browser_ui/3_create/repository/add_file_template_spec.rb
index 5b4ffadf0ffdb..e42d538fdf827 100644
--- a/qa/qa/specs/features/browser_ui/3_create/repository/add_file_template_spec.rb
+++ b/qa/qa/specs/features/browser_ui/3_create/repository/add_file_template_spec.rb
@@ -6,8 +6,10 @@ module QA
       include Runtime::Fixtures
 
       def login
-        Runtime::Browser.visit(:gitlab, Page::Main::Login)
-        Page::Main::Login.perform(&:sign_in_using_credentials)
+        unless Page::Main::Menu.perform(&:signed_in?)
+          Runtime::Browser.visit(:gitlab, Page::Main::Login)
+          Page::Main::Login.perform(&:sign_in_using_credentials)
+        end
       end
 
       before(:all) do
diff --git a/qa/spec/resource/user_spec.rb b/qa/spec/resource/user_spec.rb
index d612dfc530e60..5845f7996a30a 100644
--- a/qa/spec/resource/user_spec.rb
+++ b/qa/spec/resource/user_spec.rb
@@ -35,8 +35,8 @@
   end
 
   describe '#name' do
-    it 'defaults to the username' do
-      expect(subject.name).to eq(subject.username)
+    it 'defaults to a name based on the username' do
+      expect(subject.name).to match(/#{subject.username.tr('-', ' ')}/i)
     end
 
     it 'retrieves the name from the api_resource if present' do
-- 
GitLab