-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathplans_controller_spec.rb
More file actions
126 lines (101 loc) · 4.86 KB
/
Copy pathplans_controller_spec.rb
File metadata and controls
126 lines (101 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Api::V2::PlansController do
include ApiHelper
include Mocks::ApiV2JsonSamples
include Webmocks
include IdentifierHelper
context 'OAuth (authorization_code grant type) — on behalf of a user' do
before do
@user = create(:user)
@client = create(:oauth_application)
token = mock_authorization_code_token(oauth_application: @client, user: @user).token
@headers = {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: "Bearer #{token}"
}
end
def fetch_plans_json_response
get(api_v2_plans_path, headers: @headers)
expect(response).to render_template('api/v2/_standard_response')
expect(response).to render_template('api/v2/plans/index')
JSON.parse(response.body).with_indifferent_access
end
describe 'GET /api/v2/plans (index)' do
context 'an invalid API token is included' do
it 'returns a 401 and the expected Oauth 2.0 headers' do
# Swap actual token with a random string
@headers['Authorization'] = "Bearer #{SecureRandom.uuid}"
get(api_v2_plans_path, headers: @headers)
expect(response.code).to eql('401')
expect(response.body).to be_empty
# Expect Doorkeeper to return the standard OAuth 2.0 WWW-Authenticate header for invalid tokens
expect(response.headers['WWW-Authenticate']).to match(
/Bearer realm="Doorkeeper", error="invalid_token", error_description="The access token is invalid"/
)
end
end
context 'a valid API token is included' do
let(:json) { fetch_plans_json_response }
it 'returns a 200 and the expected response body' do
# Items array is empty
expect(json[:items]).to eq([])
# total_items reflects that nothing is returned
expect(json[:total_items]).to eq(0)
# Status code and message are correct
expect(json[:code]).to eq(200)
expect(json[:message]).to eq('OK')
# Application and source are present and sensible
expect(json[:application]).to eq(ApplicationService.application_name)
expect(json[:source]).to eq('GET /api/v2/plans')
# Time is present and parseable
expect { Time.iso8601(json[:time]) }.not_to raise_error
# Caller is included
expect(json[:caller]).to eq(@client.name)
end
it 'returns an empty array if no plans are available' do
# Items array is empty
expect(json[:items]).to eq([])
# total_items reflects that nothing is returned
expect(json[:total_items]).to eq(0)
end
it 'returns the expected plans' do
# See `app/policies/api/v2/plans_policy.rb for plans included/excluded via `GET api/v2/plans`
# Create the included plans
included_plans = [create(:plan, org: @user.org), create(:plan)]
included_plans[0].add_user!(@user.id, :creator)
# Add multiple roles for testing (ensure duplicate plans will not returned)
included_plans[1].add_user!(@user.id, :editor)
included_plans[1].add_user!(@user.id, :commenter)
# Created the excluded plans
create(:plan, :creator, org: @user.org)
inactive_plan = create(:plan, :creator)
inactive_plan.add_user!(@user.id, :editor)
Role.where(plan_id: inactive_plan.id, user_id: @user.id).update!(active: false)
expect(json[:items].length).to be(included_plans.length)
# Api::V2::PlanPresenter.identifier uses api_v2_plan_url(@plan) to set the "identifier".
# That url is constructed using `request.host` / "www.example.com"
# api_v2_plan_url(@plan) within this test will construct the url via
# default_url_options[:host] / "example.org"
# Because the urls are misaligned, we will only compare the paths here.
# TODO: Consider aligning default_url_options[:host] (in test.rb) with `request.host`
returned_identifiers = json[:items].map { |item| item[:dmp][:dmp_id][:identifier] }
returned_paths = returned_identifiers.map { |url| URI(url).path }
expected_paths = included_plans.map { |plan| api_v2_plan_path(plan) }
expect(returned_paths).to eq(expected_paths)
end
it 'allows for paging' do
original_page_size = Rails.configuration.x.application.api_max_page_size
Rails.configuration.x.application.api_max_page_size = 10
create_list(:plan, 11, :publicly_visible) do |plan|
plan.add_user!(@user.id, :commenter)
end
json = fetch_plans_json_response
test_paging(json: json, headers: @headers)
Rails.configuration.x.application.api_max_page_size = original_page_size
end
end
end
end
end