Skip to content

Commit

Permalink
Model for many-to-many with department and oauth_application.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Guo committed Jan 5, 2024
1 parent 59e2195 commit 5d6bd33
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/models/department.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ class Department < ApplicationRecord
belongs_to :managed_by_department, class_name: :Department, optional: true
has_many :managed_departments, class_name: :Department, foreign_key: :managed_by_department_id

has_many :department_allowed_applications, dependent: :destroy
has_many :oauth_applications, through: :department_allowed_applications

def all_managed_department_ids
all_ids = ids = [id]
loop do
Expand Down
4 changes: 4 additions & 0 deletions app/models/department_allowed_application.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class DepartmentAllowedApplication < ApplicationRecord
belongs_to :department
belongs_to :oauth_application, class_name: 'Doorkeeper::Application'
end
1 change: 1 addition & 0 deletions app/models/doorkeeper_application.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class DoorkeeperApplication < Doorkeeper::Application
has_many :user_allowed_applications, foreign_key: :oauth_application_id
has_many :department_allowed_applications, foreign_key: :oauth_application_id
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateDepartmentAllowedApplications < ActiveRecord::Migration[7.1]
def change
create_table :department_allowed_applications do |t|
t.references :department, null: false, foreign_key: true
t.references :oauth_application, null: false, foreign_key: true

t.timestamps
end
end
end
13 changes: 12 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2021_03_26_082831) do
ActiveRecord::Schema[7.1].define(version: 2024_01_05_091805) do
create_table "allowlisted_jwts", force: :cascade do |t|
t.string "jti", null: false
t.string "aud", null: false
Expand All @@ -20,6 +20,15 @@
t.index ["user_id"], name: "index_allowlisted_jwts_on_user_id"
end

create_table "department_allowed_applications", force: :cascade do |t|
t.integer "department_id", null: false
t.integer "oauth_application_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["department_id"], name: "index_department_allowed_applications_on_department_id"
t.index ["oauth_application_id"], name: "index_department_allowed_applications_on_oauth_application_id"
end

create_table "department_users", force: :cascade do |t|
t.integer "department_id", null: false
t.integer "user_id", null: false
Expand Down Expand Up @@ -182,6 +191,8 @@
end

add_foreign_key "allowlisted_jwts", "users", on_delete: :cascade
add_foreign_key "department_allowed_applications", "departments"
add_foreign_key "department_allowed_applications", "oauth_applications"
add_foreign_key "oauth_access_grants", "oauth_applications", column: "application_id"
add_foreign_key "oauth_access_grants", "users", column: "resource_owner_id"
add_foreign_key "oauth_access_tokens", "oauth_applications", column: "application_id"
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/department_allowed_applications.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
thape_it_department_use_oauth_app_test:
department_id: 2 # thape_it_department
oauth_application_id: 1 # oauth_app_test

thape_operation_group_use_oauth_app_test:
department_id: 7 # thape_operation_group
oauth_application_id: 1 # oauth_app_test
15 changes: 15 additions & 0 deletions test/models/department_allowed_application_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'test_helper'

class DepartmentAllowedApplicationTest < ActiveSupport::TestCase
test 'Thape IT department department_allowed_applications valid' do
it_department = departments(:thape_it_department)
assert it_department.department_allowed_applications.all(&:valid?)
assert_equal it_department.department_allowed_applications.count, 1
end

test 'Thape operation_group department_allowed_applications valid' do
oauth_app_test = oauth_applications(:oauth_app_test)
assert oauth_app_test.department_allowed_applications.all(&:valid?)
assert_equal oauth_app_test.department_allowed_applications.count, 2
end
end

0 comments on commit 5d6bd33

Please sign in to comment.