sls_adf 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,61 @@
1
+ RSpec.describe SlsAdf::Query do
2
+ let!(:stub) do
3
+ stub_request(:post, SlsAdf.configuration.graphql_url).
4
+ to_return(status: 200, body: JSON.dump('data' => response_hash))
5
+ end
6
+
7
+ describe '.context' do
8
+ let(:uuid) { SecureRandom.uuid }
9
+ let(:response_hash) { { 'context' => {} } }
10
+ subject { SlsAdf::Query.context(uuid) }
11
+
12
+ it 'makes a http request' do
13
+ subject
14
+ expect(stub).to have_been_requested
15
+ end
16
+ end
17
+
18
+ describe '.user' do
19
+ let(:id) { 'random-id' }
20
+ let(:response_hash) { { 'user' => {} } }
21
+ subject { SlsAdf::Query.user(id) }
22
+
23
+ it 'makes a http request' do
24
+ subject
25
+ expect(stub).to have_been_requested
26
+ end
27
+ end
28
+
29
+ describe '.subject_group' do
30
+ let(:uuid) { SecureRandom.uuid }
31
+ let(:response_hash) { { 'subjectGroup' => {} } }
32
+ subject { SlsAdf::Query.subject_group(uuid) }
33
+
34
+ it 'makes a http request' do
35
+ subject
36
+ expect(stub).to have_been_requested
37
+ end
38
+ end
39
+
40
+ describe '.assignment' do
41
+ let(:uuid) { SecureRandom.uuid }
42
+ let(:response_hash) { { 'assignment' => {} } }
43
+ subject { SlsAdf::Query.subject_group(uuid) }
44
+
45
+ it 'makes a http request' do
46
+ subject
47
+ expect(stub).to have_been_requested
48
+ end
49
+ end
50
+
51
+ describe '.task' do
52
+ let(:uuid) { SecureRandom.uuid }
53
+ let(:response_hash) { { 'task' => {} } }
54
+ subject { SlsAdf::Query.subject_group(uuid) }
55
+
56
+ it 'makes a http request' do
57
+ subject
58
+ expect(stub).to have_been_requested
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,5 @@
1
+ RSpec.describe SlsAdf do
2
+ it "has a version number" do
3
+ expect(SlsAdf::VERSION).not_to be nil
4
+ end
5
+ end
@@ -0,0 +1,96 @@
1
+ RSpec.describe SlsAdf::Util::Adapter do
2
+ let(:adapter) { SlsAdf::Util::Adapter.new }
3
+ describe '.execute' do
4
+ let(:document) do
5
+ GraphQL.parse(<<-'GRAPHQL')
6
+ query getCharacter($id: ID!) {
7
+ character(id: $id) {
8
+ name
9
+ }
10
+ }
11
+ GRAPHQL
12
+ end
13
+ let(:operation_name) { 'getCharacter' }
14
+ let(:variables) { { 'id' => '1001' } }
15
+ subject do
16
+ adapter.execute(
17
+ document: document, operation_name: operation_name, variables: variables
18
+ )
19
+ end
20
+
21
+ context 'when response is successful' do
22
+ let!(:stub) do
23
+ stub_request(:post, SlsAdf.configuration.graphql_url).
24
+ to_return(status: 200, body: JSON.dump(response))
25
+ end
26
+ let(:response) do
27
+ { 'data' => { 'character' => { 'name' => 'Darth Vader' } } }
28
+ end
29
+
30
+ it { is_expected.to include(response) }
31
+ it { is_expected.to include(http_status: 200) }
32
+ end
33
+
34
+ context 'when request is sent' do
35
+ let!(:stub) do
36
+ stub_request(:post, SlsAdf.configuration.graphql_url).
37
+ with(headers: request_headers, body: JSON.dump(request_body)).
38
+ to_return(status: 200, body: JSON.dump('data' => 'foo'))
39
+ end
40
+ let(:request_headers) do
41
+ {
42
+ 'Accept' => 'application/json',
43
+ 'Content-Type' => 'application/json',
44
+ 'Authorization' => 'Bearer ' + SlsAdf::CLIENT_TOKEN
45
+ }
46
+ end
47
+ let(:request_body) do
48
+ {
49
+ 'query' => document.to_query_string,
50
+ 'operationName' => operation_name,
51
+ 'variables' => variables
52
+ }
53
+ end
54
+
55
+ it 'constructs the correct request headers and body' do
56
+ subject
57
+ expect(stub).to have_been_requested
58
+ end
59
+
60
+ context 'when the first response is unauthorised' do
61
+ let!(:stub) do
62
+ stub_request(:post, SlsAdf.configuration.graphql_url).
63
+ with(headers: request_headers, body: JSON.dump(request_body)).
64
+ to_return(
65
+ { status: 401, body: JSON.dump('errors' => 'wrong token') },
66
+ { status: 200, body: JSON.dump('data' => 'correct') }
67
+ )
68
+ end
69
+
70
+ it 'makes the same call after refreshing the token' do
71
+ subject
72
+ expect(stub).to have_been_requested.twice
73
+ end
74
+ end
75
+ end
76
+
77
+ context 'when the request times out' do
78
+ let!(:stub) { stub_request(:post, SlsAdf.configuration.graphql_url).to_timeout }
79
+
80
+ it 'returns an error response' do
81
+ expect(subject).to have_key(:errors)
82
+ end
83
+ end
84
+
85
+ context 'when the response is malformed' do
86
+ let!(:stub) do
87
+ stub_request(:post, SlsAdf.configuration.graphql_url).
88
+ to_return(status: 200, body: 'wrong')
89
+ end
90
+
91
+ it 'returns an error response' do
92
+ expect(subject).to have_key(:errors)
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,95 @@
1
+ RSpec.describe SlsAdf::Util::Token do
2
+ def expected_response_body
3
+ JSON.dump(data: { token: SlsAdf::CLIENT_TOKEN })
4
+ end
5
+
6
+ def expected_request_body
7
+ { 'clientId' => SlsAdf.configuration.client_id,
8
+ 'clientSecret' => SlsAdf.configuration.client_secret,
9
+ 'grantType' => 'client_credentials',
10
+ 'scope' => 'all' }
11
+ end
12
+
13
+ describe '.token' do
14
+ let!(:stub) do
15
+ stub_request(:post, SlsAdf.configuration.get_token_url).
16
+ to_return(status: 200, body: expected_response_body)
17
+ end
18
+ subject { SlsAdf::Util::Token.token }
19
+
20
+ it 'returns the correct token' do
21
+ expect(subject).to eq SlsAdf::CLIENT_TOKEN
22
+ expect(stub).to have_been_requested
23
+ end
24
+
25
+ context 'when token has been pre-loaded' do
26
+ before { subject }
27
+ it 'does not make another API call to load the token' do
28
+ # Clears stub and request history
29
+ WebMock.reset!
30
+ # Subject is not used as it doesn't invoke the actual object's method again.
31
+ SlsAdf::Util::Token.token
32
+ expect(a_request(:any, SlsAdf.configuration.get_token_url)).
33
+ not_to have_been_made
34
+ end
35
+ end
36
+ end
37
+
38
+ describe '.refresh_token' do
39
+ let!(:stub) do
40
+ stub_request(:post, SlsAdf.configuration.get_token_url).
41
+ to_return(status: http_response_code, body: expected_response_body)
42
+ end
43
+ let(:http_response_code) { 200 }
44
+ subject { SlsAdf::Util::Token.refresh_token }
45
+
46
+ it 'returns the correct token' do
47
+ expect(subject).to eq SlsAdf::CLIENT_TOKEN
48
+ expect(stub).to have_been_requested
49
+ end
50
+
51
+ it 'makes the right API call to the get token url' do
52
+ stub =
53
+ stub_request(:post, SlsAdf.configuration.get_token_url).
54
+ with(
55
+ headers: { 'Content-Type' => 'application/json' },
56
+ body: expected_request_body
57
+ ).
58
+ to_return(status: 200, body: expected_response_body)
59
+ subject
60
+ expect(stub).to have_been_requested
61
+ end
62
+
63
+ context 'when token has been preloaded' do
64
+ before { subject }
65
+ it 'makes another API call to load the token' do
66
+ # Clears stub and request history
67
+ WebMock.reset!
68
+
69
+ new_stub = stub_request(:post, SlsAdf.configuration.get_token_url).
70
+ to_return(status: 200, body: expected_response_body)
71
+ # Subject is not used as it doesn't invoke the actual object's method again.
72
+ SlsAdf::Util::Token.refresh_token
73
+ expect(new_stub).to have_been_requested
74
+ end
75
+ end
76
+
77
+ context 'when API response is malformed' do
78
+ let!(:stub) do
79
+ stub_request(:post, SlsAdf.configuration.get_token_url).
80
+ to_return(status: 200, body: '400 Error')
81
+ end
82
+
83
+ it 'returns an empty string as the token' do
84
+ expect(subject).to eq('')
85
+ end
86
+ end
87
+
88
+ context 'when API response does not have http status code 200' do
89
+ let(:http_response_code) { 400 }
90
+ it 'returns an empty string as the token' do
91
+ expect(subject).to eq('')
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,20 @@
1
+ require 'coverage_helper'
2
+
3
+ require 'bundler/setup'
4
+ require 'webmock/rspec'
5
+ require 'sls_adf'
6
+
7
+ # Load all files within the support folder
8
+ Dir[__dir__ + '/support/**/*'].each { |f| require f if File.file?(f) }
9
+
10
+ RSpec.configure do |config|
11
+ # Enable flags like --only-failures and --next-failure
12
+ config.example_status_persistence_file_path = ".rspec_status"
13
+
14
+ # Disable RSpec exposing methods globally on `Module` and `main`
15
+ config.disable_monkey_patching!
16
+
17
+ config.expect_with :rspec do |c|
18
+ c.syntax = :expect
19
+ end
20
+ end
@@ -0,0 +1,13 @@
1
+ # Defines the method to initialise gem configuration for testing.
2
+ module SlsAdf
3
+ def self.initialise_sls_adf_gem!
4
+ SlsAdf.configure do |c|
5
+ c.graphql_url = 'https://p0nmxpxpx0.lp.gql.zone/graphql'
6
+ c.get_token_url = 'https://example.com/token'
7
+ c.client_id = 'test_client_id'
8
+ c.client_secret = 'test_client_secret'
9
+ end
10
+ end
11
+ end
12
+
13
+ SlsAdf.initialise_sls_adf_gem!
@@ -0,0 +1,24 @@
1
+ # This helper initialises the stubbed version of the Token.
2
+ module SlsAdf
3
+ CLIENT_TOKEN = 'some_token'.freeze
4
+
5
+ module Util
6
+ class StubbedToken
7
+ class << self
8
+ def token
9
+ SlsAdf::CLIENT_TOKEN
10
+ end
11
+
12
+ def refresh_token
13
+ SlsAdf::CLIENT_TOKEN
14
+ end
15
+ end
16
+ end
17
+
18
+ class Adapter
19
+ def token
20
+ @token ||= StubbedToken
21
+ end
22
+ end
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,211 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sls_adf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Toh Weiqing
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-01-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: graphql-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.12'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: typhoeus
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: dotenv
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rdoc
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: webmock
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: Ruby client library for SLS's Application Development Framework (in progress).
140
+ email:
141
+ - hello@estl.moe
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".codeclimate.yml"
147
+ - ".env.example"
148
+ - ".gitignore"
149
+ - ".hound.yml"
150
+ - ".rspec"
151
+ - ".rubocop.yml"
152
+ - ".travis.yml"
153
+ - Gemfile
154
+ - Gemfile.lock
155
+ - LICENSE
156
+ - README.md
157
+ - Rakefile
158
+ - bin/console
159
+ - bin/setup
160
+ - lib/sls_adf.rb
161
+ - lib/sls_adf/base.rb
162
+ - lib/sls_adf/configuration.rb
163
+ - lib/sls_adf/mutation.rb
164
+ - lib/sls_adf/query.rb
165
+ - lib/sls_adf/schema/schema.json
166
+ - lib/sls_adf/template.rb
167
+ - lib/sls_adf/template/fragment.rb
168
+ - lib/sls_adf/template/mutation.rb
169
+ - lib/sls_adf/template/query.rb
170
+ - lib/sls_adf/util.rb
171
+ - lib/sls_adf/util/adapter.rb
172
+ - lib/sls_adf/util/token.rb
173
+ - lib/sls_adf/version.rb
174
+ - sls_adf.gemspec
175
+ - spec/coverage_helper.rb
176
+ - spec/lib/base_spec.rb
177
+ - spec/lib/configuration_spec.rb
178
+ - spec/lib/mutation_spec.rb
179
+ - spec/lib/query_spec.rb
180
+ - spec/lib/sls_adf_spec.rb
181
+ - spec/lib/util/adapter_spec.rb
182
+ - spec/lib/util/token_spec.rb
183
+ - spec/spec_helper.rb
184
+ - spec/support/configuration_helper.rb
185
+ - spec/support/token_helper.rb
186
+ homepage: https://github.com/moexmen/sls-adf
187
+ licenses:
188
+ - MIT
189
+ metadata:
190
+ allowed_push_host: https://rubygems.org
191
+ post_install_message:
192
+ rdoc_options: []
193
+ require_paths:
194
+ - lib
195
+ required_ruby_version: !ruby/object:Gem::Requirement
196
+ requirements:
197
+ - - ">="
198
+ - !ruby/object:Gem::Version
199
+ version: 2.4.0
200
+ required_rubygems_version: !ruby/object:Gem::Requirement
201
+ requirements:
202
+ - - ">="
203
+ - !ruby/object:Gem::Version
204
+ version: '0'
205
+ requirements: []
206
+ rubyforge_project:
207
+ rubygems_version: 2.6.8
208
+ signing_key:
209
+ specification_version: 4
210
+ summary: Ruby client library for SLS (in progress).
211
+ test_files: []