From cec230bdf5c7206570c7828769d8e5481e7c4720 Mon Sep 17 00:00:00 2001
From: Semyon Pupkov <mail@semyonpupkov.com>
Date: Tue, 27 Dec 2016 00:36:33 +0500
Subject: [PATCH] Move admin groups spinach tests to rspec

https://gitlab.com/gitlab-org/gitlab-ce/issues/23036
---
 features/admin/groups.feature            |  49 --------
 features/steps/admin/groups.rb           | 143 -----------------------
 features/steps/shared/paths.rb           |   4 -
 spec/features/admin/admin_groups_spec.rb | 113 +++++++++++++++++-
 4 files changed, 111 insertions(+), 198 deletions(-)
 delete mode 100644 features/admin/groups.feature
 delete mode 100644 features/steps/admin/groups.rb

diff --git a/features/admin/groups.feature b/features/admin/groups.feature
deleted file mode 100644
index 657e847cf4a..00000000000
--- a/features/admin/groups.feature
+++ /dev/null
@@ -1,49 +0,0 @@
-@admin
-Feature: Admin Groups
-  Background:
-    Given I sign in as an admin
-    And I have group with projects
-    And User "John Doe" exists
-    And I visit admin groups page
-
-  Scenario: See group list
-    Then I should be all groups
-
-  Scenario: Create a group
-    When I click new group link
-    And submit form with new group info
-    Then I should be redirected to group page
-    And I should see newly created group
-
-  @javascript
-  Scenario: Add user into projects in group
-    When I visit admin group page
-    When I select user "John Doe" from user list as "Reporter"
-    Then I should see "John Doe" in team list in every project as "Reporter"
-
-  Scenario: Shared projects
-    Given group has shared projects
-    When I visit group page
-    Then I should see project shared with group
-
-  @javascript
-  Scenario: Invite user to a group by e-mail
-    When I visit admin group page
-    When I select user "johndoe@gitlab.com" from user list as "Reporter"
-    Then I should see "johndoe@gitlab.com" in team list in every project as "Reporter"
-
-  @javascript
-  Scenario: Signed in admin should be able to add himself to a group
-    Given "John Doe" is owner of group "Owned"
-    When I visit group "Owned" members page
-    When I select current user as "Developer"
-    Then I should see current user as "Developer"
-
-  @javascript
-  Scenario: Signed in admin should be able to remove himself from group
-    Given current user is developer of group "Owned"
-    When I visit group "Owned" members page
-    Then I should see current user as "Developer"
-    When I click on the "Remove User From Group" button for current user
-    When I visit group "Owned" members page
-    Then I should not see current user as "Developer"
diff --git a/features/steps/admin/groups.rb b/features/steps/admin/groups.rb
deleted file mode 100644
index 9396a76f0a2..00000000000
--- a/features/steps/admin/groups.rb
+++ /dev/null
@@ -1,143 +0,0 @@
-class Spinach::Features::AdminGroups < Spinach::FeatureSteps
-  include SharedAuthentication
-  include SharedGroup
-  include SharedPaths
-  include SharedUser
-  include SharedActiveTab
-  include Select2Helper
-
-  When 'I visit admin group page' do
-    visit admin_group_path(current_group)
-  end
-
-  When 'I click new group link' do
-    click_link "New Group"
-  end
-
-  step 'I have group with projects' do
-    @group   = create(:group)
-    @project = create(:project, group: @group)
-    @event   = create(:closed_issue_event, project: @project)
-
-    @project.team << [current_user, :master]
-  end
-
-  step 'submit form with new group info' do
-    fill_in 'group_path', with: 'gitlab'
-    fill_in 'group_description', with: 'Group description'
-    click_button "Create group"
-  end
-
-  step 'I should see newly created group' do
-    expect(page).to have_content "Group: gitlab"
-    expect(page).to have_content "Group description"
-  end
-
-  step 'I should be redirected to group page' do
-    expect(current_path).to eq admin_group_path(Group.find_by(path: 'gitlab'))
-  end
-
-  When 'I select user "John Doe" from user list as "Reporter"' do
-    select2(user_john.id, from: "#user_ids", multiple: true)
-    page.within "#new_project_member" do
-      select "Reporter", from: "access_level"
-    end
-    click_button "Add users to group"
-  end
-
-  When 'I select user "johndoe@gitlab.com" from user list as "Reporter"' do
-    select2('johndoe@gitlab.com', from: "#user_ids", multiple: true)
-    page.within "#new_project_member" do
-      select "Reporter", from: "access_level"
-    end
-    click_button "Add users to group"
-  end
-
-  step 'I should see "John Doe" in team list in every project as "Reporter"' do
-    page.within ".group-users-list" do
-      expect(page).to have_content "John Doe"
-      expect(page).to have_content "Reporter"
-    end
-  end
-
-  step 'I should see "johndoe@gitlab.com" in team list in every project as "Reporter"' do
-    page.within ".group-users-list" do
-      expect(page).to have_content "johndoe@gitlab.com"
-      expect(page).to have_content "Invited by"
-      expect(page).to have_content "Reporter"
-    end
-  end
-
-  step 'I should be all groups' do
-    Group.all.each do |group|
-      expect(page).to have_content group.name
-    end
-  end
-
-  step 'group has shared projects' do
-    share_link = shared_project.project_group_links.new(group_access: Gitlab::Access::MASTER)
-    share_link.group_id = current_group.id
-    share_link.save!
-  end
-
-  step 'I visit group page' do
-    visit admin_group_path(current_group)
-  end
-
-  step 'I should see project shared with group' do
-    expect(page).to have_content(shared_project.name_with_namespace)
-    expect(page).to have_content "Projects shared with"
-  end
-
-  step 'we have user "John Doe" in group' do
-    current_group.add_reporter(user_john)
-  end
-
-  step 'I should not see "John Doe" in team list' do
-    page.within ".group-users-list" do
-      expect(page).not_to have_content "John Doe"
-    end
-  end
-
-  step 'I select current user as "Developer"' do
-    page.within ".users-group-form" do
-      select2(current_user.id, from: "#user_ids", multiple: true)
-      select "Developer", from: "access_level"
-    end
-
-    click_button "Add to group"
-  end
-
-  step 'I should see current user as "Developer"' do
-    page.within '.content-list' do
-      expect(page).to have_content(current_user.name)
-      expect(page).to have_content('Developer')
-    end
-  end
-
-  step 'I click on the "Remove User From Group" button for current user' do
-    find(:css, 'li', text: current_user.name).find(:css, 'a.btn-remove').click
-    # poltergeist always confirms popups.
-  end
-
-  step 'I should not see current user as "Developer"' do
-    page.within '.content-list' do
-      expect(page).not_to have_content(current_user.name)
-      expect(page).not_to have_content('Developer')
-    end
-  end
-
-  protected
-
-  def current_group
-    @group ||= Group.first
-  end
-
-  def shared_project
-    @shared_project ||= create(:empty_project)
-  end
-
-  def user_john
-    @user_john ||= User.find_by(name: "John Doe")
-  end
-end
diff --git a/features/steps/shared/paths.rb b/features/steps/shared/paths.rb
index 15b81fa529b..670e6ca49a3 100644
--- a/features/steps/shared/paths.rb
+++ b/features/steps/shared/paths.rb
@@ -191,10 +191,6 @@ module SharedPaths
     visit admin_background_jobs_path
   end
 
-  step 'I visit admin groups page' do
-    visit admin_groups_path
-  end
-
   step 'I visit admin teams page' do
     visit admin_teams_path
   end
diff --git a/spec/features/admin/admin_groups_spec.rb b/spec/features/admin/admin_groups_spec.rb
index 0aa01fc499a..afd42962b69 100644
--- a/spec/features/admin/admin_groups_spec.rb
+++ b/spec/features/admin/admin_groups_spec.rb
@@ -1,15 +1,39 @@
 require 'spec_helper'
 
 feature 'Admin Groups', feature: true do
+  include Select2Helper
+
   let(:internal) { Gitlab::VisibilityLevel::INTERNAL }
+  let(:user) { create :user }
+  let!(:group) { create :group }
+  let!(:current_user) { login_as :admin }
 
   before do
-    login_as(:admin)
-
     stub_application_setting(default_group_visibility: internal)
   end
 
+  describe 'list' do
+    it 'renders groups' do
+      visit admin_groups_path
+
+      expect(page).to have_content(group.name)
+    end
+  end
+
   describe 'create a group' do
+    it 'creates new group' do
+      visit admin_groups_path
+
+      click_link "New Group"
+      fill_in 'group_path', with: 'gitlab'
+      fill_in 'group_description', with: 'Group description'
+      click_button "Create group"
+
+      expect(current_path).to eq admin_group_path(Group.find_by(path: 'gitlab'))
+      expect(page).to have_content('Group: gitlab')
+      expect(page).to have_content('Group description')
+    end
+
     scenario 'shows the visibility level radio populated with the default value' do
       visit new_admin_group_path
 
@@ -27,6 +51,91 @@ feature 'Admin Groups', feature: true do
     end
   end
 
+  describe 'add user into a group', js: true do
+    shared_context 'adds user into a group' do
+      it do
+        visit admin_group_path(group)
+
+        select2(user_selector, from: '#user_ids', multiple: true)
+        page.within '#new_project_member' do
+          select2(Gitlab::Access::REPORTER, from: '#access_level')
+        end
+        click_button "Add users to group"
+        page.within ".group-users-list" do
+          expect(page).to have_content(user.name)
+          expect(page).to have_content('Reporter')
+        end
+      end
+    end
+
+    it_behaves_like 'adds user into a group' do
+      let(:user_selector) { user.id }
+    end
+
+    it_behaves_like 'adds user into a group' do
+      let(:user_selector) { user.email }
+    end
+  end
+
+  describe 'add admin himself to a group' do
+    before do
+      group.add_user(:user, Gitlab::Access::OWNER)
+    end
+
+    it 'adds admin a to a group as developer', js: true do
+      visit group_group_members_path(group)
+
+      page.within '.users-group-form' do
+        select2(current_user.id, from: '#user_ids', multiple: true)
+        select 'Developer', from: 'access_level'
+      end
+
+      click_button 'Add to group'
+
+      page.within '.content-list' do
+        expect(page).to have_content(current_user.name)
+        expect(page).to have_content('Developer')
+      end
+    end
+  end
+
+  describe 'admin remove himself from a group', js: true do
+    it 'removes admin from the group' do
+      group.add_user(current_user, Gitlab::Access::DEVELOPER)
+
+      visit group_group_members_path(group)
+
+      page.within '.content-list' do
+        expect(page).to have_content(current_user.name)
+        expect(page).to have_content('Developer')
+      end
+
+      find(:css, 'li', text: current_user.name).find(:css, 'a.btn-remove').click
+
+      visit group_group_members_path(group)
+
+      page.within '.content-list' do
+        expect(page).not_to have_content(current_user.name)
+        expect(page).not_to have_content('Developer')
+      end
+    end
+  end
+
+  describe 'shared projects' do
+    it 'renders shared project' do
+      empty_project = create(:empty_project)
+      empty_project.project_group_links.create!(
+        group_access: Gitlab::Access::MASTER,
+        group: group
+      )
+
+      visit admin_group_path(group)
+
+      expect(page).to have_content(empty_project.name_with_namespace)
+      expect(page).to have_content('Projects shared with')
+    end
+  end
+
   def expect_selected_visibility(level)
     selector = "#group_visibility_level_#{level}[checked=checked]"
 
-- 
GitLab