|
| 1 | +require "spec_helper" |
| 2 | + |
| 3 | +RSpec.describe ScimRails::ScimResourceController, type: :controller do |
| 4 | + include AuthHelper |
| 5 | + include CallbackHelper |
| 6 | + |
| 7 | + let!(:counter) { CallbackHelper::CallbackCounter.new } |
| 8 | + |
| 9 | + routes { ScimRails::Engine.routes } |
| 10 | + |
| 11 | + describe "resource_user" do |
| 12 | + let(:company) { create(:company) } |
| 13 | + |
| 14 | + context "when unauthorized" do |
| 15 | + it "returns scim+json content type" do |
| 16 | + get :resource_user |
| 17 | + |
| 18 | + expect(response.content_type).to eq("application/scim+json") |
| 19 | + end |
| 20 | + |
| 21 | + it "fails with no credentials" do |
| 22 | + get :resource_user |
| 23 | + |
| 24 | + expect(response.status).to eq(401) |
| 25 | + end |
| 26 | + |
| 27 | + it "fails with invalid credentials" do |
| 28 | + request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials("unauthorized","123456") |
| 29 | + |
| 30 | + get :resource_user |
| 31 | + |
| 32 | + expect(response.status).to eq(401) |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + context "when authorized" do |
| 37 | + let(:body) { JSON.parse(response.body) } |
| 38 | + |
| 39 | + before :each do |
| 40 | + http_login(company) |
| 41 | + end |
| 42 | + |
| 43 | + it "returns scim+json content type" do |
| 44 | + get :resource_user |
| 45 | + |
| 46 | + expect(response.content_type).to eq("application/scim+json") |
| 47 | + end |
| 48 | + |
| 49 | + it "is successful with valid credentials" do |
| 50 | + get :resource_user |
| 51 | + |
| 52 | + expect(response.status).to eq(200) |
| 53 | + end |
| 54 | + |
| 55 | + it "successfully returns the resource schema of users" do |
| 56 | + get :resource_user |
| 57 | + |
| 58 | + expect(body.deep_symbolize_keys).to eq(ScimRails.config.resource_user_schema) |
| 59 | + end |
| 60 | + |
| 61 | + context "when before_scim_response is defined" do |
| 62 | + before do |
| 63 | + ScimRails.config.before_scim_response = lambda do |body| |
| 64 | + counter.before.call |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + after do |
| 69 | + ScimRails.config.before_scim_response = nil |
| 70 | + end |
| 71 | + |
| 72 | + it "successfully calls before_scim_response" do |
| 73 | + expect{ get :resource_user }.to change{ counter.before_called }.from(0).to(1) |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + context "when after_scim_response is defined" do |
| 78 | + before do |
| 79 | + ScimRails.config.after_scim_response = lambda do |object, status| |
| 80 | + counter.after.call |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + after do |
| 85 | + ScimRails.config.after_scim_response = nil |
| 86 | + end |
| 87 | + |
| 88 | + it "successfully calls after_scim_response" do |
| 89 | + expect{ get :resource_user }.to change{ counter.after_called }.from(0).to(1) |
| 90 | + end |
| 91 | + end |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + describe "resource_group" do |
| 96 | + let(:company) { create(:company) } |
| 97 | + |
| 98 | + context "when unauthorized" do |
| 99 | + it "returns scim+json content type" do |
| 100 | + get :resource_group |
| 101 | + |
| 102 | + expect(response.content_type).to eq("application/scim+json") |
| 103 | + end |
| 104 | + |
| 105 | + it "fails with no credentials" do |
| 106 | + get :resource_group |
| 107 | + |
| 108 | + expect(response.status).to eq(401) |
| 109 | + end |
| 110 | + |
| 111 | + it "fails with invalid credentials" do |
| 112 | + request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials("unauthorized","123456") |
| 113 | + |
| 114 | + get :resource_group |
| 115 | + |
| 116 | + expect(response.status).to eq(401) |
| 117 | + end |
| 118 | + end |
| 119 | + |
| 120 | + context "when authorized" do |
| 121 | + let(:body) { JSON.parse(response.body) } |
| 122 | + |
| 123 | + before :each do |
| 124 | + http_login(company) |
| 125 | + end |
| 126 | + |
| 127 | + it "returns scim+json content type" do |
| 128 | + get :resource_group |
| 129 | + |
| 130 | + expect(response.content_type).to eq("application/scim+json") |
| 131 | + end |
| 132 | + |
| 133 | + it "is successful with valid credentials" do |
| 134 | + get :resource_group |
| 135 | + |
| 136 | + expect(response.status).to eq(200) |
| 137 | + end |
| 138 | + |
| 139 | + it "successfully returns the resource schema of groups" do |
| 140 | + get :resource_group |
| 141 | + |
| 142 | + expect(body.deep_symbolize_keys).to eq(ScimRails.config.resource_group_schema) |
| 143 | + end |
| 144 | + |
| 145 | + context "when before_scim_response is defined" do |
| 146 | + before do |
| 147 | + ScimRails.config.before_scim_response = lambda do |body| |
| 148 | + counter.before.call |
| 149 | + end |
| 150 | + end |
| 151 | + |
| 152 | + after do |
| 153 | + ScimRails.config.before_scim_response = nil |
| 154 | + end |
| 155 | + |
| 156 | + it "successfully calls before_scim_response" do |
| 157 | + expect{ get :resource_group }.to change{ counter.before_called }.from(0).to(1) |
| 158 | + end |
| 159 | + end |
| 160 | + |
| 161 | + context "when after_scim_response is defined" do |
| 162 | + before do |
| 163 | + ScimRails.config.after_scim_response = lambda do |object, status| |
| 164 | + counter.after.call |
| 165 | + end |
| 166 | + end |
| 167 | + |
| 168 | + after do |
| 169 | + ScimRails.config.after_scim_response = nil |
| 170 | + end |
| 171 | + |
| 172 | + it "successfully calls before_scim_response" do |
| 173 | + expect{ get :resource_group }.to change{ counter.after_called }.from(0).to(1) |
| 174 | + end |
| 175 | + end |
| 176 | + end |
| 177 | + end |
| 178 | +end |
0 commit comments