Skip to content

Commit

Permalink
cleanup tests, add unauthorized test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisHuynh333 committed Dec 20, 2024
1 parent 802eb62 commit 1fb24d4
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 172 deletions.
1 change: 1 addition & 0 deletions app/controllers/concerns/bot_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def create # rubocop:disable Metrics/MethodLength
end

def new_destroy
authorize! @namespace, to: :destroy_bot_accounts?
render turbo_stream: turbo_stream.update('bot_modal',
partial: 'new_destroy_modal',
locals: {
Expand Down
114 changes: 35 additions & 79 deletions test/controllers/concerns/bot_actions_concern_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,28 @@
class BotActionsConcernTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers

test 'project bot accounts index' do
setup do
sign_in users(:john_doe)
@namespace = groups(:group_one)
@project = projects(:project1)
@project_bot = namespace_bots(:project1_bot0)
@group_bot = namespace_bots(:group1_bot0)
end

namespace = groups(:group_one)
project = projects(:project1)

get namespace_project_bots_path(namespace, project)
test 'project bot accounts index' do
get namespace_project_bots_path(@namespace, @project)

assert_response :success
end

test 'new project bot account' do
sign_in users(:john_doe)

namespace = groups(:group_one)
project = projects(:project1)

get new_namespace_project_bot_path(namespace, project, format: :turbo_stream)
get new_namespace_project_bot_path(@namespace, @project, format: :turbo_stream)

assert_response :success
end

test 'project bot account create' do
sign_in users(:john_doe)

namespace = groups(:group_one)
project = projects(:project1)

post namespace_project_bots_path(namespace, project, format: :turbo_stream),
post namespace_project_bots_path(@namespace, @project, format: :turbo_stream),
params: { bot: {
token_name: 'newtesttoken',
access_level: Member::AccessLevel::UPLOADER,
Expand All @@ -44,12 +37,7 @@ class BotActionsConcernTest < ActionDispatch::IntegrationTest
end

test 'project bot account create error' do
sign_in users(:john_doe)

namespace = groups(:group_one)
project = projects(:project1)

post namespace_project_bots_path(namespace, project, format: :turbo_stream),
post namespace_project_bots_path(@namespace, @project, format: :turbo_stream),
params: { bot: {
access_level: Member::AccessLevel::UPLOADER,
scopes: ['read_api']
Expand All @@ -59,55 +47,33 @@ class BotActionsConcernTest < ActionDispatch::IntegrationTest
end

test 'project bot account destroy' do
sign_in users(:john_doe)

namespace_bot = namespace_bots(:project1_bot0)

namespace = groups(:group_one)
project = projects(:project1)

delete namespace_project_bot_path(namespace, project, id: namespace_bot.id, format: :turbo_stream)
delete namespace_project_bot_path(@namespace, @project, id: @project_bot.id, format: :turbo_stream)

assert_response :redirect
end

test 'project bot account destroy error' do
sign_in users(:john_doe)
project2 = projects(:project2)

namespace = groups(:group_one)
project = projects(:project2)

delete namespace_project_bot_path(namespace, project, id: 0, format: :turbo_stream)
delete namespace_project_bot_path(@namespace, project2, id: 0, format: :turbo_stream)

assert_response :not_found
end

test 'group bot accounts index' do
sign_in users(:john_doe)

namespace = groups(:group_one)

get group_bots_path(namespace)
get group_bots_path(@namespace)

assert_response :success
end

test 'new group bot account' do
sign_in users(:john_doe)

namespace = groups(:group_one)

get new_group_bot_path(namespace, format: :turbo_stream)
get new_group_bot_path(@namespace, format: :turbo_stream)

assert_response :success
end

test 'group bot account create' do
sign_in users(:john_doe)

namespace = groups(:group_one)

post group_bots_path(namespace, format: :turbo_stream),
post group_bots_path(@namespace, format: :turbo_stream),
params: { bot: {
token_name: 'newtesttoken',
access_level: Member::AccessLevel::UPLOADER,
Expand All @@ -118,11 +84,7 @@ class BotActionsConcernTest < ActionDispatch::IntegrationTest
end

test 'group bot account create error' do
sign_in users(:john_doe)

namespace = groups(:group_one)

post group_bots_path(namespace, format: :turbo_stream),
post group_bots_path(@namespace, format: :turbo_stream),
params: { bot: {
access_level: Member::AccessLevel::UPLOADER,
scopes: ['read_api']
Expand All @@ -132,48 +94,42 @@ class BotActionsConcernTest < ActionDispatch::IntegrationTest
end

test 'group bot account destroy' do
sign_in users(:john_doe)

namespace_bot = namespace_bots(:group1_bot0)

namespace = groups(:group_one)

delete group_bot_path(namespace, id: namespace_bot.id, format: :turbo_stream)
delete group_bot_path(@namespace, id: @group_bot.id, format: :turbo_stream)

assert_response :redirect
end

test 'group bot account destroy error' do
sign_in users(:john_doe)

namespace = groups(:group_one)

delete group_bot_path(namespace, id: 0, format: :turbo_stream)
delete group_bot_path(@namespace, id: 0, format: :turbo_stream)

assert_response :not_found
end

test 'new_destroy in group' do
sign_in users(:john_doe)
get group_bot_new_destroy_path(@namespace, bot_id: @group_bot.id)

namespace = groups(:group_one)
namespace_bot = namespace_bots(:group1_bot0)
assert_response :success
end

get group_bot_new_destroy_path(namespace, bot_id: namespace_bot.id)
test 'unauthorized new_destroy in group' do
sign_in users(:ryan_doe)

assert_response :success
get group_bot_new_destroy_path(@namespace, bot_id: @group_bot.id)

assert_response :unauthorized
end

test 'new_destroy in project' do
sign_in users(:john_doe)
get namespace_project_bot_new_destroy_path(@namespace, @project, bot_id: @project_bot.id)

namespace_bot = namespace_bots(:project1_bot0)
assert_response :success
end

namespace = groups(:group_one)
project = projects(:project1)
test 'unauthorized new_destroy in project' do
sign_in users(:ryan_doe)

get namespace_project_bot_new_destroy_path(namespace, project, bot_id: namespace_bot.id)
get namespace_project_bot_new_destroy_path(@namespace, @project, bot_id: @project_bot.id)

assert_response :success
assert_response :unauthorized
end
end
Loading

0 comments on commit 1fb24d4

Please sign in to comment.