2023-06-14 09:48:57 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
2024-09-04 07:12:25 +02:00
|
|
|
RSpec.describe 'Credentials' do
|
2023-06-14 09:48:57 +02:00
|
|
|
describe 'GET /api/v1/apps/verify_credentials' do
|
|
|
|
subject do
|
|
|
|
get '/api/v1/apps/verify_credentials', headers: headers
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an oauth token' do
|
2023-10-18 14:10:07 +02:00
|
|
|
let(:application) { Fabricate(:application, scopes: 'read') }
|
|
|
|
let(:token) { Fabricate(:accessible_access_token, application: application) }
|
2023-06-14 09:48:57 +02:00
|
|
|
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
|
|
|
|
|
2023-10-13 14:42:09 +02:00
|
|
|
it 'returns the app information correctly', :aggregate_failures do
|
2023-06-14 09:48:57 +02:00
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
2024-09-20 15:13:04 +02:00
|
|
|
expect(response.content_type)
|
|
|
|
.to start_with('application/json')
|
2023-06-14 09:48:57 +02:00
|
|
|
|
2024-09-06 11:58:46 +02:00
|
|
|
expect(response.parsed_body).to match(
|
2023-06-14 09:48:57 +02:00
|
|
|
a_hash_including(
|
2024-05-17 15:46:12 +02:00
|
|
|
id: token.application.id.to_s,
|
2023-06-14 09:48:57 +02:00
|
|
|
name: token.application.name,
|
|
|
|
website: token.application.website,
|
2023-10-18 14:10:07 +02:00
|
|
|
scopes: token.application.scopes.map(&:to_s),
|
2024-05-17 15:46:12 +02:00
|
|
|
redirect_uris: token.application.redirect_uris,
|
|
|
|
# Deprecated properties as of 4.3:
|
|
|
|
redirect_uri: token.application.redirect_uri.split.first,
|
|
|
|
vapid_key: Rails.configuration.x.vapid_public_key
|
2023-10-18 14:10:07 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
2024-05-17 15:46:12 +02:00
|
|
|
|
|
|
|
it 'does not expose the client_id or client_secret' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
2024-09-20 15:13:04 +02:00
|
|
|
expect(response.content_type)
|
|
|
|
.to start_with('application/json')
|
2024-05-17 15:46:12 +02:00
|
|
|
|
2024-09-23 10:42:52 +02:00
|
|
|
expect(response.parsed_body)
|
|
|
|
.to not_include(client_id: be_present)
|
|
|
|
.and not_include(client_secret: be_present)
|
2024-05-17 15:46:12 +02:00
|
|
|
end
|
2023-10-18 14:10:07 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a non-read scoped oauth token' do
|
|
|
|
let(:application) { Fabricate(:application, scopes: 'admin:write') }
|
|
|
|
let(:token) { Fabricate(:accessible_access_token, application: application) }
|
|
|
|
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
|
|
|
|
|
2024-09-19 12:15:21 +02:00
|
|
|
it 'returns http success and returns app information' do
|
2023-10-18 14:10:07 +02:00
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
2024-09-20 15:13:04 +02:00
|
|
|
expect(response.content_type)
|
|
|
|
.to start_with('application/json')
|
2023-10-18 14:10:07 +02:00
|
|
|
|
2024-09-06 11:58:46 +02:00
|
|
|
expect(response.parsed_body).to match(
|
2023-10-18 14:10:07 +02:00
|
|
|
a_hash_including(
|
2024-05-17 15:46:12 +02:00
|
|
|
id: token.application.id.to_s,
|
2023-10-18 14:10:07 +02:00
|
|
|
name: token.application.name,
|
|
|
|
website: token.application.website,
|
|
|
|
scopes: token.application.scopes.map(&:to_s),
|
2024-05-17 15:46:12 +02:00
|
|
|
redirect_uris: token.application.redirect_uris,
|
|
|
|
# Deprecated properties as of 4.3:
|
|
|
|
redirect_uri: token.application.redirect_uri.split.first,
|
|
|
|
vapid_key: Rails.configuration.x.vapid_public_key
|
2023-06-14 09:48:57 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without an oauth token' do
|
|
|
|
let(:headers) { {} }
|
|
|
|
|
|
|
|
it 'returns http unauthorized' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(401)
|
2024-09-20 15:13:04 +02:00
|
|
|
expect(response.content_type)
|
|
|
|
.to start_with('application/json')
|
2023-06-14 09:48:57 +02:00
|
|
|
end
|
|
|
|
end
|
2023-10-18 14:10:07 +02:00
|
|
|
|
|
|
|
context 'with a revoked oauth token' do
|
|
|
|
let(:application) { Fabricate(:application, scopes: 'read') }
|
|
|
|
let(:token) { Fabricate(:accessible_access_token, application: application, revoked_at: DateTime.now.utc) }
|
|
|
|
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
|
|
|
|
|
|
|
|
it 'returns http authorization error' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(401)
|
2024-09-20 15:13:04 +02:00
|
|
|
expect(response.content_type)
|
|
|
|
.to start_with('application/json')
|
2023-10-18 14:10:07 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the error in the json response' do
|
|
|
|
subject
|
|
|
|
|
2024-09-06 11:58:46 +02:00
|
|
|
expect(response.parsed_body).to match(
|
2023-10-18 14:10:07 +02:00
|
|
|
a_hash_including(
|
|
|
|
error: 'The access token was revoked'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an invalid oauth token' do
|
|
|
|
let(:application) { Fabricate(:application, scopes: 'read') }
|
|
|
|
let(:token) { Fabricate(:accessible_access_token, application: application) }
|
|
|
|
let(:headers) { { 'Authorization' => "Bearer #{token.token}-invalid" } }
|
|
|
|
|
2024-09-19 12:15:21 +02:00
|
|
|
it 'returns http authorization error with json error' do
|
2023-10-18 14:10:07 +02:00
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(401)
|
2024-09-20 15:13:04 +02:00
|
|
|
expect(response.content_type)
|
|
|
|
.to start_with('application/json')
|
2023-10-18 14:10:07 +02:00
|
|
|
|
2024-09-06 11:58:46 +02:00
|
|
|
expect(response.parsed_body).to match(
|
2023-10-18 14:10:07 +02:00
|
|
|
a_hash_including(
|
|
|
|
error: 'The access token is invalid'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2023-06-14 09:48:57 +02:00
|
|
|
end
|
|
|
|
end
|