2021-07-01 07:24:35 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
require "test_helper"
|
2021-04-27 07:06:45 +00:00
|
|
|
|
2021-04-29 06:42:24 +00:00
|
|
|
class AppUsersControllerTest < 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" })
|
|
|
|
|
@org_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
|
|
|
@org_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
|
|
|
@org_viewer = User.create({ first_name: "Viewer", email: "viewer@example.com", password: "password",
|
2021-04-29 06:41:23 +00:00
|
|
|
organization: @org })
|
2021-04-27 09:24:21 +00:00
|
|
|
|
2021-07-01 07:24:35 +00:00
|
|
|
@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")
|
2021-04-27 09:24:21 +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")
|
2021-04-29 06:41:23 +00:00
|
|
|
|
|
|
|
|
@first_app = App.create({
|
2021-07-01 07:24:35 +00:00
|
|
|
name: "Test App",
|
2021-04-29 06:41:23 +00:00
|
|
|
organization: @org_admin.organization
|
|
|
|
|
})
|
|
|
|
|
end
|
2021-04-27 09:24:21 +00:00
|
|
|
|
2021-07-01 07:24:35 +00:00
|
|
|
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,
|
2021-07-01 07:24:35 +00:00
|
|
|
role: "admin"
|
2021-04-29 06:41:23 +00:00
|
|
|
}, as: :json, headers: auth_header(@org_admin)
|
2021-04-27 09:24:21 +00:00
|
|
|
end
|
2021-04-29 06:41:23 +00:00
|
|
|
end
|
2021-04-27 09:24:21 +00:00
|
|
|
|
2021-07-01 07:24:35 +00:00
|
|
|
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,
|
2021-07-01 07:24:35 +00:00
|
|
|
role: "admin"
|
2021-04-29 06:41:23 +00:00
|
|
|
}, as: :json, headers: auth_header(@org_developer)
|
2021-04-27 09:24:21 +00:00
|
|
|
end
|
2021-04-29 06:41:23 +00:00
|
|
|
end
|
2021-04-27 09:24:21 +00:00
|
|
|
|
2021-07-01 07:24:35 +00:00
|
|
|
test "app admins can create app users" do
|
|
|
|
|
AppUser.create(user: @org_viewer, app: @first_app, role: "admin")
|
2021-04-27 09:24:21 +00:00
|
|
|
|
2021-07-01 07:24:35 +00:00
|
|
|
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,
|
2021-07-01 07:24:35 +00:00
|
|
|
role: "admin"
|
2021-04-29 06:41:23 +00:00
|
|
|
}, as: :json, headers: auth_header(@org_viewer)
|
2021-04-27 09:24:21 +00:00
|
|
|
end
|
2021-04-29 06:41:23 +00:00
|
|
|
end
|
2021-04-27 09:24:21 +00:00
|
|
|
|
2021-07-01 07:24:35 +00:00
|
|
|
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,
|
2021-07-01 07:24:35 +00:00
|
|
|
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
|