ToolJet/test/controllers/app_users_controller_test.rb

73 lines
2.8 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "test_helper"
2021-04-27 07:06:45 +00:00
class AppUsersControllerTest < ActionDispatch::IntegrationTest
2021-04-29 06:41:23 +00:00
def setup
@org = Organization.create({ name: "ToolJet Test" })
@org_admin = User.create({ first_name: "Admin", email: "admin@example.com", password: "password",
2021-04-29 06:41:23 +00:00
organization: @org })
@org_developer = User.create({ first_name: "Dev", email: "dev@example.com", password: "password",
2021-04-29 06:41:23 +00:00
organization: @org })
@org_viewer = User.create({ first_name: "Viewer", email: "viewer@example.com", password: "password",
2021-04-29 06:41:23 +00:00
organization: @org })
@org_user_admin = OrganizationUser.create(organization: @org, user: @org_admin, role: "admin", status: "active")
@org_user_developer = OrganizationUser.create(organization: @org, user: @org_developer, role: "developer")
@org_user_viewer = OrganizationUser.create(organization: @org, user: @org_viewer, role: "viewer", status: "active")
@another_org = Organization.create({ name: "Another ToolJet Test" })
@another_org_admin = User.create({ first_name: "Admin", email: "admin@domain.com", password: "password",
2021-04-29 06:41:23 +00:00
organization: @another_org })
@org_another_org_admin = OrganizationUser.create(organization: @another_org, user: @another_org_admin,
role: "admin")
2021-04-29 06:41:23 +00:00
@first_app = App.create({
name: "Test App",
2021-04-29 06:41:23 +00:00
organization: @org_admin.organization
})
end
test "org admins can create app users" do
assert_difference "AppUser.count", 1 do
post "/app_users/", params: {
2021-04-29 06:41:23 +00:00
org_user_id: @org_user_admin.id,
app_id: @first_app.id,
role: "admin"
2021-04-29 06:41:23 +00:00
}, as: :json, headers: auth_header(@org_admin)
end
2021-04-29 06:41:23 +00:00
end
test "non org-admins cannot create app users" do
assert_no_difference "AppUser.count" do
post "/app_users/", params: {
2021-04-29 06:41:23 +00:00
org_user_id: @org_user_developer.id,
app_id: @first_app.id,
role: "admin"
2021-04-29 06:41:23 +00:00
}, as: :json, headers: auth_header(@org_developer)
end
2021-04-29 06:41:23 +00:00
end
test "app admins can create app users" do
AppUser.create(user: @org_viewer, app: @first_app, role: "admin")
assert_difference "AppUser.count", 1 do
post "/app_users/", params: {
2021-04-29 06:41:23 +00:00
org_user_id: @org_user_developer.id,
app_id: @first_app.id,
role: "admin"
2021-04-29 06:41:23 +00:00
}, as: :json, headers: auth_header(@org_viewer)
end
2021-04-29 06:41:23 +00:00
end
test "admin of another org cannot create app users" do
assert_no_difference "AppUser.count" do
post "/app_users/", params: {
2021-04-29 06:41:23 +00:00
org_user_id: @org_user_admin.id,
app_id: @first_app.id,
role: "admin"
2021-04-29 06:41:23 +00:00
}, as: :json, headers: auth_header(@another_org_admin)
end
end
2021-04-27 07:06:45 +00:00
end