Skip to content

Commit 38affd8

Browse files
authored
Merge pull request joshsoftware#152 from BandanaPandey/webmock_test
refs joshsoftware#149 Added delete button on admin users page.
2 parents 9099afc + 79682c1 commit 38affd8

File tree

6 files changed

+68
-10
lines changed

6 files changed

+68
-10
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,4 +420,4 @@ RUBY VERSION
420420
ruby 2.3.0p0
421421

422422
BUNDLED WITH
423-
1.12.5
423+
1.12.5

app/controllers/admin/users_controller.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,17 @@ def search
3333

3434
render :index
3535
end
36+
37+
def destroy
38+
user = User.find(params[:id])
39+
40+
if user
41+
user.destroy
42+
redirect_to admin_users_path, notice: "#{user.name} has been deleted successfully"
43+
else
44+
redirect_to admin_users_path, notice: "User not found"
45+
end
46+
47+
end
48+
3649
end

app/models/user.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,20 @@ class User
5353
field :last_repo_sync_at, type: Time
5454
field :last_gh_data_sync_at, type: Time
5555

56+
belongs_to :goal
5657
has_many :commits, dependent: :destroy
5758
has_many :activities, dependent: :destroy
59+
has_many :transactions, dependent: :destroy
60+
has_many :subscriptions, dependent: :destroy
61+
has_many :rounds
62+
has_many :comments, dependent: :destroy
63+
has_many :redeem_requests, dependent: :destroy
64+
has_many :group_invitations, dependent: :destroy
5865
has_and_belongs_to_many :repositories, class_name: 'Repository', inverse_of: 'users'
5966
has_and_belongs_to_many :judges_repositories, class_name: 'Repository', inverse_of: 'judges'
6067
has_and_belongs_to_many :roles, inverse_of: nil
61-
has_many :transactions
62-
has_many :subscriptions
63-
has_many :rounds
64-
has_many :comments
6568
has_and_belongs_to_many :organizations
66-
has_and_belongs_to_many :groups, class_name: 'Group', inverse_of: 'members'
67-
has_many :redeem_requests
68-
has_many :group_invitations
69-
belongs_to :goal
69+
has_and_belongs_to_many :groups, class_name: 'Group', inverse_of: 'members'
7070

7171
slug :github_handle
7272

app/views/admin/users/_user.html.haml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@
1616
%a{href: admin_user_login_as_path(user), class: "btn btn-primary btn-sm", data: {confirm: "Are you sure?"}}
1717
%i.fa.fa-user
1818
Login as
19+
%a{href: admin_user_path(user.id), class: "btn btn-danger btn-sm", data: {method: 'delete', confirm: "Are you sure? This action is irreversible"}}
20+
%i.fa.fa-trash

config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
get :assign_judge
4040
end
4141
end
42-
resources :users, only: [:index] do
42+
resources :users, only: [:index, :destroy] do
4343
get :mark_as_judge
4444
get :login_as
4545
get :search, on: :collection
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require "test_helper"
2+
3+
class Admin::UsersControllerTest < ActionController::TestCase
4+
5+
def setup
6+
super
7+
@admin_role = create :role, :admin
8+
@goal = create :goal, points: 15
9+
@round = create :round, :open
10+
@user = create :user, auth_token: 'dah123rty', goal: @goal
11+
end
12+
13+
test 'should not access index if the current user is non-admin' do
14+
sign_in @user
15+
get :index
16+
assert_response :redirect
17+
end
18+
19+
test 'should access users index only if the user is admin' do
20+
@user.roles << @admin_role
21+
sign_in @user
22+
other_user = create :user, auth_token: Faker::Lorem.word, goal: @goal
23+
assert_equal User.count, 2
24+
get :index
25+
assert_response :success
26+
end
27+
28+
test 'should destroy all associations of the user on destroying any user by admin' do
29+
@user.roles << @admin_role
30+
sign_in @user
31+
other_user = create :user, auth_token: 'dah123rty', goal: @goal
32+
assert_equal other_user.transactions.count, 0
33+
transaction = create_list :transaction, 3, type: 'credit', user: other_user
34+
assert_equal other_user.transactions.count, 3
35+
redeem_request = create :redeem_request, points: 2, user: other_user
36+
assert_equal other_user.transactions.count, 4
37+
delete :destroy, id: other_user.id
38+
assert_equal Transaction.count, 0
39+
assert_equal RedeemRequest.count, 0
40+
assert_response :redirect
41+
end
42+
43+
end

0 commit comments

Comments
 (0)