2021-07-01 07:24:35 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
require "test_helper"
|
2021-04-23 06:17:24 +00:00
|
|
|
|
|
|
|
|
class OrganizationUsersControllerTest < ActionDispatch::IntegrationTest
|
2021-04-29 06:41:23 +00:00
|
|
|
def setup
|
2021-07-01 07:24:35 +00:00
|
|
|
@org = Organization.create({ name: "ToolJet Test" })
|
|
|
|
|
@admin = User.create({ first_name: "Admin", email: "admin@example.com", password: "password",
|
2021-04-29 06:41:23 +00:00
|
|
|
organization: @org })
|
2021-07-01 07:24:35 +00:00
|
|
|
@developer = User.create({ first_name: "Dev", email: "dev@example.com", password: "password",
|
2021-04-29 06:41:23 +00:00
|
|
|
organization: @org })
|
2021-07-01 07:24:35 +00:00
|
|
|
@viewer = User.create({ first_name: "Viewer", email: "viewer@example.com", password: "password",
|
2021-04-29 06:41:23 +00:00
|
|
|
organization: @org })
|
2021-07-01 07:24:35 +00:00
|
|
|
@org_user_admin = OrganizationUser.create(organization: @org, user: @admin, role: "admin", status: "active")
|
|
|
|
|
@org_user_developer = OrganizationUser.create(organization: @org, user: @developer, role: "developer", status: "active")
|
|
|
|
|
@org_user_viewer = OrganizationUser.create(organization: @org, user: @viewer, role: "viewer", status: "active")
|
2021-04-25 14:07:13 +00:00
|
|
|
|
2021-07-01 07:24:35 +00:00
|
|
|
@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,
|
2021-07-01 07:24:35 +00:00
|
|
|
role: "admin", status: "active")
|
2021-04-29 06:41:23 +00:00
|
|
|
end
|
2021-04-25 14:07:13 +00:00
|
|
|
|
2021-06-12 02:21:57 +00:00
|
|
|
# POST /create tests
|
|
|
|
|
|
2021-07-01 07:24:35 +00:00
|
|
|
test "org admins can create org users" do
|
|
|
|
|
assert_difference "OrganizationUser.count", 1 do
|
|
|
|
|
post "/organization_users", params: org_user_params, as: :json, headers: auth_header(@admin)
|
2021-04-25 14:07:13 +00:00
|
|
|
end
|
2021-04-29 06:41:23 +00:00
|
|
|
end
|
2021-04-25 14:07:13 +00:00
|
|
|
|
2021-07-01 07:24:35 +00:00
|
|
|
test "org admins cannot create org users if email already exists" do
|
|
|
|
|
post "/organization_users", params: org_user_params, as: :json, headers: auth_header(@admin)
|
|
|
|
|
post "/organization_users", params: org_user_params, as: :json, headers: auth_header(@admin)
|
2021-07-01 06:47:48 +00:00
|
|
|
|
|
|
|
|
assert_response 422
|
2021-07-01 07:24:35 +00:00
|
|
|
assert_equal "Email address is already taken", JSON.parse(response.body)["message"]
|
2021-07-01 06:47:48 +00:00
|
|
|
end
|
|
|
|
|
|
2021-07-01 07:24:35 +00:00
|
|
|
test "OrganizationUser should be unique per organization and user" do
|
2021-07-01 06:47:48 +00:00
|
|
|
assert_raises(ActiveRecord::RecordNotUnique) do
|
2021-07-01 07:24:35 +00:00
|
|
|
org_user = OrganizationUser.new(organization: @org, user: @admin, role: "admin", status: "active")
|
2021-07-01 06:47:48 +00:00
|
|
|
org_user.save
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-07-01 07:24:35 +00:00
|
|
|
test "cannot create org users if not admin" do
|
|
|
|
|
assert_no_difference "OrganizationUser.count" do
|
|
|
|
|
post "/organization_users", params: org_user_params, as: :json, headers: auth_header(@developer)
|
2021-04-25 14:07:13 +00:00
|
|
|
end
|
|
|
|
|
|
2021-07-01 07:24:35 +00:00
|
|
|
assert_no_difference "OrganizationUser.count" do
|
|
|
|
|
post "/organization_users", params: org_user_params, as: :json, headers: auth_header(@viewer)
|
2021-04-25 14:07:13 +00:00
|
|
|
end
|
2021-04-29 06:41:23 +00:00
|
|
|
end
|
2021-04-25 14:07:13 +00:00
|
|
|
|
2021-06-12 02:21:57 +00:00
|
|
|
# POST /change_role tests
|
|
|
|
|
|
2021-07-01 07:24:35 +00:00
|
|
|
test "org admins can change role of org users" do
|
|
|
|
|
assert_equal "developer", @org_user_developer.role
|
|
|
|
|
post organization_user_change_role_url(@org_user_developer.id), params: { role: "viewer" }, as: :json,
|
2021-04-29 06:41:23 +00:00
|
|
|
headers: auth_header(@admin)
|
|
|
|
|
assert_response 204
|
2021-07-01 07:24:35 +00:00
|
|
|
assert_equal "viewer", @org_user_developer.reload.role
|
2021-04-29 06:41:23 +00:00
|
|
|
end
|
2021-04-25 14:07:13 +00:00
|
|
|
|
2021-07-01 07:24:35 +00:00
|
|
|
test "cannot change role of org users if not org admin" do
|
|
|
|
|
assert_equal "developer", @org_user_developer.role
|
|
|
|
|
post organization_user_change_role_url(@org_user_developer.id), params: { role: "viewer" }, as: :json,
|
2021-04-29 06:41:23 +00:00
|
|
|
headers: auth_header(@viewer)
|
|
|
|
|
assert_response 403
|
2021-07-01 07:24:35 +00:00
|
|
|
assert_equal "developer", @org_user_developer.reload.role
|
2021-04-25 14:07:13 +00:00
|
|
|
|
2021-07-01 07:24:35 +00:00
|
|
|
assert_equal "viewer", @org_user_viewer.role
|
|
|
|
|
post organization_user_change_role_url(@org_user_viewer.id), params: { role: "viewer" }, as: :json,
|
2021-04-29 06:41:23 +00:00
|
|
|
headers: auth_header(@developer)
|
|
|
|
|
assert_response 403
|
2021-07-01 07:24:35 +00:00
|
|
|
assert_equal "viewer", @org_user_viewer.reload.role
|
2021-04-29 06:41:23 +00:00
|
|
|
end
|
|
|
|
|
|
2021-07-01 07:24:35 +00:00
|
|
|
test "org users of one org cannot change role of users of another org" do
|
|
|
|
|
assert_equal "admin", @org_another_org_admin.role
|
|
|
|
|
post organization_user_change_role_url(@org_another_org_admin.id), params: { role: "viewer" }, as: :json,
|
2021-04-29 06:41:23 +00:00
|
|
|
headers: auth_header(@admin)
|
|
|
|
|
assert_response 403
|
2021-07-01 07:24:35 +00:00
|
|
|
assert_equal "admin", @org_another_org_admin.reload.role
|
2021-04-29 06:41:23 +00:00
|
|
|
end
|
|
|
|
|
|
2021-06-12 02:21:57 +00:00
|
|
|
## POST /archive tests
|
|
|
|
|
|
2021-07-01 07:24:35 +00:00
|
|
|
test "org admins can archive org users" do
|
|
|
|
|
assert_equal "active", @org_user_developer.status
|
2021-06-12 02:21:57 +00:00
|
|
|
post organization_user_archive_url(@org_user_developer.id), as: :json, headers: auth_header(@admin)
|
|
|
|
|
|
|
|
|
|
assert_response 204
|
2021-07-01 07:24:35 +00:00
|
|
|
assert_equal "archived", @org_user_developer.reload.status
|
2021-06-12 02:21:57 +00:00
|
|
|
end
|
|
|
|
|
|
2021-07-01 07:24:35 +00:00
|
|
|
test "cannot archive user if not org admin" do
|
|
|
|
|
assert_equal "active", @org_user_developer.status
|
2021-06-12 02:21:57 +00:00
|
|
|
post organization_user_archive_url(@org_user_developer.id), as: :json, headers: auth_header(@viewer)
|
|
|
|
|
assert_response 403
|
2021-07-01 07:24:35 +00:00
|
|
|
assert_equal "active", @org_user_developer.reload.status
|
2021-06-12 02:21:57 +00:00
|
|
|
|
2021-07-01 07:24:35 +00:00
|
|
|
assert_equal "active", @org_user_viewer.status
|
2021-06-12 02:21:57 +00:00
|
|
|
post organization_user_archive_url(@org_user_viewer.id), as: :json, headers: auth_header(@developer)
|
|
|
|
|
assert_response 403
|
2021-07-01 07:24:35 +00:00
|
|
|
assert_equal "active", @org_user_viewer.reload.status
|
2021-06-12 02:21:57 +00:00
|
|
|
end
|
|
|
|
|
|
2021-07-01 07:24:35 +00:00
|
|
|
test "cannot archive user of another org" do
|
|
|
|
|
assert_equal "active", @org_another_org_admin.status
|
2021-06-12 02:21:57 +00:00
|
|
|
post organization_user_change_role_url(@org_another_org_admin.id), as: :json, headers: auth_header(@admin)
|
|
|
|
|
assert_response 403
|
2021-07-01 07:24:35 +00:00
|
|
|
assert_equal "active", @org_another_org_admin.reload.status
|
2021-06-12 02:21:57 +00:00
|
|
|
end
|
|
|
|
|
|
2021-04-29 06:41:23 +00:00
|
|
|
private
|
2021-04-25 14:07:13 +00:00
|
|
|
|
2021-07-01 07:24:35 +00:00
|
|
|
def org_user_params
|
|
|
|
|
{
|
|
|
|
|
first_name: "test",
|
|
|
|
|
last_name: "user",
|
|
|
|
|
email: "user@example.com",
|
|
|
|
|
role: "admin"
|
|
|
|
|
}
|
|
|
|
|
end
|
2021-04-23 06:17:24 +00:00
|
|
|
end
|