Skip to content

Commit

Permalink
tenants api v1
Browse files Browse the repository at this point in the history
  • Loading branch information
stage-rl committed Dec 7, 2023
1 parent fcd5008 commit 96c06b6
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
35 changes: 35 additions & 0 deletions app/controllers/api/admin/tenants_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Api::Admin::TenantsController < ActionController::Base
before_action :set_tenant, only: %i[destroy]
def create
@tenant = Tenant.new(tenant_params)
# authorize([:admin, @tenant])
respond_to do |format|
if @tenant.save
format.json { render json: @tenant, status: :created }
else
format.json { render json: @tenant.errors, status: :unprocessable_entity }
end
end
end

def destroy
# authorize([:admin, @tenant])
respond_to do |format|
if @tenant.destroy
format.json { render json: {}, status: :ok }
else
format.json { render json: @tenant.errors, status: :unprocessable_entity }
end
end
end

private

def set_tenant
@tenant = Tenant.find(params[:id])
end

def tenant_params
params.require(:tenant).permit(:name)
end
end
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@
delete :destroy, on: :collection
end

namespace :api do
namespace :admin do
resources :tenants
end
end

get :auth, path: 'prihlasenie', to: 'sessions#login'
get 'auth/google_oauth2/callback', to: 'sessions#create'
get 'auth/google_oauth2/failure', to: 'sessions#failure'
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/group_memberships.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ one:
two:
user: basic
group: all

three:
user: solver_other
group: solver_other_user
5 changes: 5 additions & 0 deletions test/fixtures/groups.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ solver_admin:
name: admins
type: AdminGroup
tenant: solver

solver_other_user:
name: Other user
type: UserGroup
tenant: solver
19 changes: 19 additions & 0 deletions test/integration/tenant_api_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "test_helper"

class TenantApiTest < ActionDispatch::IntegrationTest
test "can create tenant" do
post "/api/admin/tenants", params: { tenant: { name: "Testovaci tenant" } }, as: :json
assert_response :success
json_response = JSON.parse(response.body)
assert_equal "Testovaci tenant", json_response["name"]
assert_not_nil json_response["id"]
assert_empty json_response["feature_flags"]
end

test "can destroy tenant" do
tenant = tenants(:solver)
delete "/api/admin/tenants/#{tenant.id}", params: {}, as: :json
assert_response :ok
json_response = JSON.parse(response.body)
end
end

0 comments on commit 96c06b6

Please sign in to comment.