|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +describe WorkOS::Role do |
| 4 | + describe '.initialize' do |
| 5 | + context 'with full role data including permissions' do |
| 6 | + it 'initializes all attributes correctly' do |
| 7 | + role_json = { |
| 8 | + id: 'role_01FAEAJCJ3P1Z6WP5Y9VQPN2XY', |
| 9 | + name: 'Admin', |
| 10 | + slug: 'admin', |
| 11 | + description: 'Administrator role with full access', |
| 12 | + permissions: ['read:users', 'write:users', 'admin:all'], |
| 13 | + type: 'system', |
| 14 | + created_at: '2022-05-13T17:45:31.732Z', |
| 15 | + updated_at: '2022-07-13T17:45:42.618Z', |
| 16 | + }.to_json |
| 17 | + |
| 18 | + role = described_class.new(role_json) |
| 19 | + |
| 20 | + expect(role.id).to eq('role_01FAEAJCJ3P1Z6WP5Y9VQPN2XY') |
| 21 | + expect(role.name).to eq('Admin') |
| 22 | + expect(role.slug).to eq('admin') |
| 23 | + expect(role.description).to eq('Administrator role with full access') |
| 24 | + expect(role.permissions).to eq(['read:users', 'write:users', 'admin:all']) |
| 25 | + expect(role.type).to eq('system') |
| 26 | + expect(role.created_at).to eq('2022-05-13T17:45:31.732Z') |
| 27 | + expect(role.updated_at).to eq('2022-07-13T17:45:42.618Z') |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + context 'with role data without permissions' do |
| 32 | + it 'initializes permissions as empty array' do |
| 33 | + role_json = { |
| 34 | + id: 'role_01FAEAJCJ3P1Z6WP5Y9VQPN2XY', |
| 35 | + name: 'User', |
| 36 | + slug: 'user', |
| 37 | + description: 'Basic user role', |
| 38 | + type: 'custom', |
| 39 | + created_at: '2022-05-13T17:45:31.732Z', |
| 40 | + updated_at: '2022-07-13T17:45:42.618Z', |
| 41 | + }.to_json |
| 42 | + |
| 43 | + role = described_class.new(role_json) |
| 44 | + |
| 45 | + expect(role.id).to eq('role_01FAEAJCJ3P1Z6WP5Y9VQPN2XY') |
| 46 | + expect(role.name).to eq('User') |
| 47 | + expect(role.slug).to eq('user') |
| 48 | + expect(role.description).to eq('Basic user role') |
| 49 | + expect(role.permissions).to eq([]) |
| 50 | + expect(role.type).to eq('custom') |
| 51 | + expect(role.created_at).to eq('2022-05-13T17:45:31.732Z') |
| 52 | + expect(role.updated_at).to eq('2022-07-13T17:45:42.618Z') |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + context 'with role data with null permissions' do |
| 57 | + it 'initializes permissions as empty array' do |
| 58 | + role_json = { |
| 59 | + id: 'role_01FAEAJCJ3P1Z6WP5Y9VQPN2XY', |
| 60 | + name: 'User', |
| 61 | + slug: 'user', |
| 62 | + description: 'Basic user role', |
| 63 | + permissions: nil, |
| 64 | + type: 'custom', |
| 65 | + created_at: '2022-05-13T17:45:31.732Z', |
| 66 | + updated_at: '2022-07-13T17:45:42.618Z', |
| 67 | + }.to_json |
| 68 | + |
| 69 | + role = described_class.new(role_json) |
| 70 | + |
| 71 | + expect(role.permissions).to eq([]) |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + context 'with role data with empty permissions array' do |
| 76 | + it 'preserves empty permissions array' do |
| 77 | + role_json = { |
| 78 | + id: 'role_01FAEAJCJ3P1Z6WP5Y9VQPN2XY', |
| 79 | + name: 'User', |
| 80 | + slug: 'user', |
| 81 | + description: 'Basic user role', |
| 82 | + permissions: [], |
| 83 | + type: 'custom', |
| 84 | + created_at: '2022-05-13T17:45:31.732Z', |
| 85 | + updated_at: '2022-07-13T17:45:42.618Z', |
| 86 | + }.to_json |
| 87 | + |
| 88 | + role = described_class.new(role_json) |
| 89 | + |
| 90 | + expect(role.permissions).to eq([]) |
| 91 | + end |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + describe '.to_json' do |
| 96 | + context 'with role that has permissions' do |
| 97 | + it 'includes permissions in serialized output' do |
| 98 | + role_json = { |
| 99 | + id: 'role_01FAEAJCJ3P1Z6WP5Y9VQPN2XY', |
| 100 | + name: 'Admin', |
| 101 | + slug: 'admin', |
| 102 | + description: 'Administrator role', |
| 103 | + permissions: ['read:all', 'write:all'], |
| 104 | + type: 'system', |
| 105 | + created_at: '2022-05-13T17:45:31.732Z', |
| 106 | + updated_at: '2022-07-13T17:45:42.618Z', |
| 107 | + }.to_json |
| 108 | + |
| 109 | + role = described_class.new(role_json) |
| 110 | + serialized = role.to_json |
| 111 | + |
| 112 | + expect(serialized[:id]).to eq('role_01FAEAJCJ3P1Z6WP5Y9VQPN2XY') |
| 113 | + expect(serialized[:name]).to eq('Admin') |
| 114 | + expect(serialized[:slug]).to eq('admin') |
| 115 | + expect(serialized[:description]).to eq('Administrator role') |
| 116 | + expect(serialized[:permissions]).to eq(['read:all', 'write:all']) |
| 117 | + expect(serialized[:type]).to eq('system') |
| 118 | + expect(serialized[:created_at]).to eq('2022-05-13T17:45:31.732Z') |
| 119 | + expect(serialized[:updated_at]).to eq('2022-07-13T17:45:42.618Z') |
| 120 | + end |
| 121 | + end |
| 122 | + |
| 123 | + context 'with role that has no permissions' do |
| 124 | + it 'includes empty permissions array in serialized output' do |
| 125 | + role_json = { |
| 126 | + id: 'role_01FAEAJCJ3P1Z6WP5Y9VQPN2XY', |
| 127 | + name: 'User', |
| 128 | + slug: 'user', |
| 129 | + description: 'Basic user role', |
| 130 | + type: 'custom', |
| 131 | + created_at: '2022-05-13T17:45:31.732Z', |
| 132 | + updated_at: '2022-07-13T17:45:42.618Z', |
| 133 | + }.to_json |
| 134 | + |
| 135 | + role = described_class.new(role_json) |
| 136 | + serialized = role.to_json |
| 137 | + |
| 138 | + expect(serialized[:permissions]).to eq([]) |
| 139 | + end |
| 140 | + end |
| 141 | + end |
| 142 | +end |
0 commit comments