main
 1# frozen_string_literal: true
 2
 3require 'rails_helper'
 4
 5describe "/scim/v2/groups" do
 6  context "when authenticated" do
 7    let(:user) { create(:user) }
 8    let(:token) { create(:access_token, subject: user) }
 9    let(:headers) do
10      {
11        'Authorization' => "Bearer #{token.to_jwt}",
12        'Accept' => 'application/scim+json',
13        'Content-Type' => 'application/scim+json',
14      }
15    end
16
17    describe "GET /scim/v2/groups" do
18      before { get '/scim/v2/groups', headers: headers }
19
20      let(:json) { JSON.parse(response.body, symbolize_names: true) }
21
22      specify { expect(response).to have_http_status(:ok) }
23      specify { expect(response.headers['Content-Type']).to eql('application/scim+json') }
24      specify { expect(response.body).to be_present }
25
26      specify { expect(json[:schemas]).to match_array([Scim::Kit::V2::Messages::LIST_RESPONSE]) }
27      specify { expect(json[:totalResults]).to be_kind_of(Numeric) }
28      specify { expect(json[:Resources]).to match_array([id: user.to_param, userName: user.email]) }
29    end
30  end
31
32  context "when the authentication token is invalid" do
33    let(:bad_headers) do
34      {
35        'Authorization' => "Bearer #{SecureRandom.uuid}",
36        'Accept' => 'application/scim+json',
37        'Content-Type' => 'application/scim+json',
38      }
39    end
40
41    before { get '/scim/v2/groups', headers: bad_headers }
42
43    specify { expect(response).to have_http_status(:unauthorized) }
44  end
45end