-
Notifications
You must be signed in to change notification settings - Fork 45
/
api_spec.rb
157 lines (128 loc) · 5.05 KB
/
api_spec.rb
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
require 'rails_helper'
require 'webmock/rspec'
RSpec.describe SUSE::Connect::Api do
let(:username) { 'scc_user' }
let(:password) { 'scc_password' }
let(:api_client) { described_class.new(username, password) }
let(:uuid) { 'test-uuid' }
describe '#system_uuid' do
subject(:method_call) { api_client.send(:system_uuid) }
context 'when system_uuid file exists' do
it 'reads a file' do
allow(File).to receive(:exist?).with(described_class::UUID_FILE_LOCATION).and_return(true)
expect(File).not_to receive(:write).with(described_class::UUID_FILE_LOCATION, uuid)
expect(File).to receive(:read).with(described_class::UUID_FILE_LOCATION).and_return(uuid)
expect(method_call).to be(uuid)
end
end
context 'when system_uuid file does not exist' do
it 'creates a file' do
allow(File).to receive(:exist?).with(described_class::UUID_FILE_LOCATION).and_return(false)
allow(SecureRandom).to receive(:uuid).and_return(uuid)
expect(File).to receive(:write).with(described_class::UUID_FILE_LOCATION, uuid).exactly(1).times
expect(File).not_to receive(:read).with(described_class::UUID_FILE_LOCATION)
expect(method_call).to be(uuid)
end
end
end
context 'api requests' do
before do
allow_any_instance_of(described_class).to receive(:system_uuid).and_return(uuid)
stub_request(:GET, 'http://example.org/api_method')
.with(headers: expected_request_headers)
.to_return(
status: 200,
body: response_data.to_json,
headers: {}
)
stub_request(:GET, 'http://example.org/api_method?page=1')
.with(headers: expected_request_headers)
.to_return(
status: 200,
body: [ response_data ].to_json,
headers: {
'Link' => '<http://example.org/api_method?page=2>; rel="next"'
}
)
stub_request(:GET, 'http://example.org/api_method?page=2')
.with(headers: expected_request_headers)
.to_return(
status: 200,
body: [ response_data ].to_json,
headers: {}
)
stub_request(:get, 'https://scc.suse.com/connect/organizations/orders?page=1')
.with(headers: expected_request_headers)
.to_return(
status: 200,
body: [ { endpoint: 'organizations/orders' } ].to_json,
headers: {}
)
stub_request(:get, 'https://scc.suse.com/connect/organizations/products?page=1')
.with(headers: expected_request_headers)
.to_return(
status: 200,
body: [ { endpoint: 'organizations/products' } ].to_json,
headers: {}
)
stub_request(:get, 'https://scc.suse.com/connect/organizations/products/unscoped?page=1')
.with(headers: expected_request_headers)
.to_return(
status: 200,
body: [ { endpoint: 'organizations/products/unscoped' } ].to_json,
headers: {}
)
stub_request(:get, 'https://scc.suse.com/connect/organizations/repositories?page=1')
.with(headers: expected_request_headers)
.to_return(
status: 200,
body: [ { endpoint: 'organizations/repositories' } ].to_json,
headers: {}
)
stub_request(:get, 'https://scc.suse.com/connect/organizations/subscriptions?page=1')
.with(headers: expected_request_headers)
.to_return(
status: 200,
body: [ { endpoint: 'organizations/subscriptions' } ].to_json,
headers: {}
)
end
let(:url) { 'http://example.com' }
let(:expected_request_headers) do
{
'Authorization' => 'Basic ' + Base64.encode64("#{username}:#{password}").strip,
'User-Agent' => "RMT/#{RMT::VERSION}",
'RMT' => uuid
}
end
let(:response_data) { { foo: 'bar' } }
describe '#make_single_request' do
subject { api_client.send(:make_single_request, 'GET', 'http://example.org/api_method') }
it { is_expected.to eq(response_data) }
end
describe '#make_paginated_request' do
subject { api_client.send(:make_paginated_request, 'GET', 'http://example.org/api_method') }
it { is_expected.to eq([response_data, response_data]) }
end
describe '#list_orders' do
subject { api_client.list_orders }
it { is_expected.to eq([ { endpoint: 'organizations/orders' } ]) }
end
describe '#list_products' do
subject { api_client.list_products }
it { is_expected.to eq([ { endpoint: 'organizations/products' } ]) }
end
describe '#list_products_unscoped' do
subject { api_client.list_products_unscoped }
it { is_expected.to eq([ { endpoint: 'organizations/products/unscoped' } ]) }
end
describe '#list_repositories' do
subject { api_client.list_repositories }
it { is_expected.to eq([ { endpoint: 'organizations/repositories' } ]) }
end
describe '#list_subscriptions' do
subject { api_client.list_subscriptions }
it { is_expected.to eq([ { endpoint: 'organizations/subscriptions' } ]) }
end
end
end