quiz_api_client 4.2.0 → 4.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/lib/quiz_api_client/services/items_service.rb +11 -0
  3. data/lib/quiz_api_client/version.rb +1 -1
  4. data/spec/config_spec.rb +66 -0
  5. data/spec/contracts/interaction_types_service_spec.rb +22 -0
  6. data/spec/contracts/item_analyses_service_spec.rb +59 -0
  7. data/spec/contracts/items_service_spec.rb +59 -0
  8. data/spec/contracts/qti_imports_service_spec.rb +34 -0
  9. data/spec/contracts/quiz_clone_job_service_spec.rb +20 -0
  10. data/spec/contracts/quiz_clone_jobs_service_spec.rb +21 -0
  11. data/spec/contracts/quiz_entries_service_spec.rb +125 -0
  12. data/spec/contracts/quiz_service_spec.rb +68 -0
  13. data/spec/contracts/quiz_session_events_service_spec.rb +30 -0
  14. data/spec/contracts/quiz_session_result_service_spec.rb +42 -0
  15. data/spec/contracts/quiz_session_service_spec.rb +56 -0
  16. data/spec/contracts/quiz_sessions_service_spec.rb +28 -0
  17. data/spec/contracts/quizzes_service_spec.rb +80 -0
  18. data/spec/contracts/session_item_results_service_spec.rb +60 -0
  19. data/spec/contracts/session_items_service_spec.rb +21 -0
  20. data/spec/contracts/shared_banks_spec.rb +366 -0
  21. data/spec/contracts/shared_examples/http_delete_example.rb +56 -0
  22. data/spec/contracts/shared_examples/http_get_example.rb +139 -0
  23. data/spec/contracts/shared_examples/http_patch_example.rb +60 -0
  24. data/spec/contracts/shared_examples/http_post_example.rb +60 -0
  25. data/spec/contracts/shared_examples/http_put_example.rb +60 -0
  26. data/spec/http_client_spec.rb +347 -0
  27. data/spec/json_formatter_spec.rb +32 -0
  28. data/spec/quiz_api_client/http_request/failure_spec.rb +100 -0
  29. data/spec/quiz_api_client/http_request/metrics_spec.rb +75 -0
  30. data/spec/quiz_api_client_spec.rb +117 -0
  31. data/spec/services/base_api_service_spec.rb +50 -0
  32. data/spec/services/interaction_types_service_spec.rb +25 -0
  33. data/spec/services/item_analyses_service_spec.rb +76 -0
  34. data/spec/services/items_service_spec.rb +56 -0
  35. data/spec/services/jwt_service_spec.rb +66 -0
  36. data/spec/services/qti_imports_service_spec.rb +114 -0
  37. data/spec/services/quiz_analyses_service_spec.rb +44 -0
  38. data/spec/services/quiz_clone_job_service_spec.rb +41 -0
  39. data/spec/services/quiz_clone_jobs_service_spec.rb +41 -0
  40. data/spec/services/quiz_entries_service_spec.rb +71 -0
  41. data/spec/services/quiz_service_spec.rb +49 -0
  42. data/spec/services/quiz_session_events_service_spec.rb +42 -0
  43. data/spec/services/quiz_session_result_service_spec.rb +26 -0
  44. data/spec/services/quiz_session_service_spec.rb +49 -0
  45. data/spec/services/quiz_sessions_service_spec.rb +42 -0
  46. data/spec/services/quizzes_service_spec.rb +71 -0
  47. data/spec/services/session_item_results_service_spec.rb +33 -0
  48. data/spec/services/session_items_service_spec.rb +26 -0
  49. data/spec/spec_helper.rb +42 -0
  50. data/spec/support/pact_config.rb +64 -0
  51. data/spec/support/pact_helper.rb +19 -0
  52. metadata +108 -39
  53. data/.dockerignore +0 -7
  54. data/.editorconfig +0 -16
  55. data/.gitignore +0 -13
  56. data/.rspec +0 -3
  57. data/.rubocop.yml +0 -72
  58. data/CHANGELOG.md +0 -35
  59. data/Dockerfile +0 -12
  60. data/Gemfile +0 -5
  61. data/Jenkinsfile +0 -86
  62. data/bin/console +0 -7
  63. data/bin/contracts-generate +0 -26
  64. data/bin/setup +0 -65
  65. data/docker-compose.dev.override.yml +0 -11
  66. data/docker-compose.yml +0 -10
  67. data/quiz_api_client.gemspec +0 -60
@@ -0,0 +1,44 @@
1
+ describe QuizApiClient::Services::QuizAnalysesService do
2
+ let(:host) { 'api.host' }
3
+ let(:config) { QuizApiClient::Config.new { |c| c.host = host } }
4
+ let(:subject) { described_class.new(config) }
5
+
6
+ describe '#list' do
7
+ let(:params) { { quiz_id: 1 } }
8
+ let(:stubbed_response) { { 'id' => '1' } }
9
+ let(:expected_url) do
10
+ "https://#{host}/api/quizzes/#{params[:quiz_id]}/stats/quiz_analysis"
11
+ end
12
+ let(:expected_body) { '' }
13
+ let(:status_code) { 200 }
14
+
15
+ before do
16
+ stub_request(:get, expected_url)
17
+ .with(body: expected_body)
18
+ .to_return(
19
+ status: status_code,
20
+ body: stubbed_response.to_json,
21
+ headers: { 'Content-Type' => 'application/json' }
22
+ )
23
+ end
24
+
25
+ it 'posts to quiz_api/api/quizzes and returns the response' do
26
+ result = subject.get(params: params, token: 'token')
27
+ expect(result.parsed_response).to eql(stubbed_response)
28
+ end
29
+
30
+ it 'raises an error when no quiz_id is specified' do
31
+ params = {}
32
+ expect do
33
+ subject.get(params: params, token: 'token')
34
+ end.to raise_error 'Quiz Id Required'
35
+ end
36
+
37
+ context 'response' do
38
+ it 'returns the response with the correct status code' do
39
+ response = subject.get(params: params, token: 'token')
40
+ expect(response.code).to eq(status_code)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,41 @@
1
+ describe QuizApiClient::Services::QuizCloneJobService do
2
+ let(:host) { 'api.host' }
3
+ let(:config) { QuizApiClient::Config.new { |c| c.host = host } }
4
+ let(:subject) { described_class.new(config) }
5
+
6
+ describe '#show' do
7
+ let(:expected_url) { "https://#{host}/api/quiz_clone_jobs/#{params[:id]}" }
8
+ let(:params) { { id: 1 } }
9
+ let(:stubbed_response) { { 'id' => 1, 'original_quiz_id' => 666, 'status' => 'completed' } }
10
+
11
+ context 'on success' do
12
+ before do
13
+ stub_request(:get, expected_url)
14
+ .to_return(
15
+ status: 200,
16
+ body: stubbed_response.to_json,
17
+ headers: { 'Content-Type' => 'application/json' }
18
+ )
19
+ end
20
+
21
+ it 'gets from /api/quiz_clone_jobs/:id' do
22
+ result = subject.show(params: params, token: 'token')
23
+ expect(result.parsed_response).to eql(stubbed_response)
24
+ end
25
+ end
26
+
27
+ context 'on failure' do
28
+ let(:status_code) { 401 }
29
+
30
+ before do
31
+ stub_request(:get, expected_url)
32
+ .to_return(status: status_code)
33
+ end
34
+
35
+ it 'returns a response with the correct code' do
36
+ response = subject.show(params: params, token: 'token')
37
+ expect(response.code).to eq(status_code)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,41 @@
1
+ describe QuizApiClient::Services::QuizCloneJobsService do
2
+ let(:host) { 'api.host' }
3
+ let(:config) { QuizApiClient::Config.new { |c| c.host = host } }
4
+ let(:subject) { described_class.new(config) }
5
+
6
+ describe '#create' do
7
+ let(:expected_url) { "https://#{host}/api/quizzes/#{params[:quiz_id]}/quiz_clone_jobs" }
8
+ let(:params) { { quiz_id: 1 } }
9
+ let(:stubbed_response) { { 'id' => '1' } }
10
+
11
+ context 'on success' do
12
+ before do
13
+ stub_request(:post, expected_url)
14
+ .to_return(
15
+ status: 200,
16
+ body: stubbed_response.to_json,
17
+ headers: { 'Content-Type' => 'application/json' }
18
+ )
19
+ end
20
+
21
+ it 'posts to /api/quizzes/:quiz_id/quiz_clone_job' do
22
+ result = subject.create(params: params, token: 'token')
23
+ expect(result.parsed_response).to eql(stubbed_response)
24
+ end
25
+ end
26
+
27
+ context 'on failure' do
28
+ let(:status_code) { 401 }
29
+
30
+ before do
31
+ stub_request(:post, expected_url)
32
+ .to_return(status: status_code)
33
+ end
34
+
35
+ it 'returns a response with the correct code' do
36
+ response = subject.create(params: params, token: 'token')
37
+ expect(response.code).to eq(status_code)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,71 @@
1
+ describe QuizApiClient::Services::QuizEntriesService do
2
+ let(:host) { 'api.host' }
3
+ let(:config) { QuizApiClient::Config.new { |c| c.host = host } }
4
+ let(:subject) { described_class.new(config) }
5
+
6
+ describe '#list' do
7
+ let(:params) { { id: 1 } }
8
+ let(:stubbed_response) { { 'id' => 1, 'entry' => { 'id' => 1 } } }
9
+ let(:expected_url) { "https://#{host}/api/quizzes/#{params[:id]}/quiz_entries" }
10
+ let(:status_code) { 200 }
11
+
12
+ before do
13
+ stub_request(:get, expected_url)
14
+ .to_return(
15
+ status: status_code,
16
+ body: stubbed_response.to_json,
17
+ headers: { 'Content-Type' => 'application/json' }
18
+ )
19
+ end
20
+
21
+ it 'gets from quiz_api/api/quizzes/{id}/quiz_entries and returns the response' do
22
+ result = subject.list(params: params, token: 'token')
23
+ expect(result.parsed_response).to eql(stubbed_response)
24
+ end
25
+ end
26
+
27
+ describe '#create' do
28
+ let(:params) do
29
+ { id: 1, entry_id: 1, entry_type: 'Item', points_possible: 1 }
30
+ end
31
+ let(:stubbed_response) { { 'id' => 1, 'entry' => { 'id' => 1 } } }
32
+ let(:expected_url) { "https://#{host}/api/quizzes/#{params[:id]}/quiz_entries" }
33
+ let(:status_code) { 201 }
34
+
35
+ before do
36
+ stub_request(:post, expected_url)
37
+ .to_return(
38
+ status: status_code,
39
+ body: stubbed_response.to_json,
40
+ headers: { 'Content-Type' => 'application/json' }
41
+ )
42
+ end
43
+
44
+ it 'posts to quiz_api/api/quizzes/{id}/quiz_entries and returns the response' do
45
+ result = subject.create(params: params, token: 'token')
46
+ expect(result.parsed_response).to eql(stubbed_response)
47
+ end
48
+ end
49
+
50
+ describe '#destroy' do
51
+ let(:params) do
52
+ { id: 6, quiz_id: 1 }
53
+ end
54
+ let(:expected_url) { "https://#{host}/api/quizzes/#{params[:quiz_id]}/quiz_entries/#{params[:id]}" }
55
+ let(:status_code) { 200 }
56
+
57
+ before do
58
+ stub_request(:delete, expected_url)
59
+ .to_return(
60
+ status: status_code,
61
+ body: nil,
62
+ headers: { 'Content-Type' => 'application/json' }
63
+ )
64
+ end
65
+
66
+ it 'issues a DELETE to quiz_api/api/quizzes/{quiz_id}/quiz_entries/{id} and returns the response' do
67
+ result = subject.destroy(params: params, token: 'token')
68
+ expect(result.parsed_response).to eql(nil)
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,49 @@
1
+ describe QuizApiClient::Services::QuizService do
2
+ let(:host) { 'api.host' }
3
+ let(:config) { QuizApiClient::Config.new { |c| c.host = host } }
4
+ let(:subject) { described_class.new(config) }
5
+
6
+ describe '#update' do
7
+ let(:params) { { id: 1, title: 'some new title' } }
8
+ let(:stubbed_response) { { 'id' => 1, 'title' => 'some new title' } }
9
+ let(:expected_url) { "https://#{host}/api/quizzes/#{params[:id]}" }
10
+ let(:expected_body) { { quiz: params }.to_json }
11
+ let(:status_code) { 200 }
12
+
13
+ before do
14
+ stub_request(:patch, expected_url)
15
+ .with(body: expected_body)
16
+ .to_return(
17
+ status: status_code,
18
+ body: stubbed_response.to_json,
19
+ headers: { 'Content-Type' => 'application/json' }
20
+ )
21
+ end
22
+
23
+ it 'patches to quiz_api/api/quizzes and returns the response' do
24
+ result = subject.update(params: params, token: 'token')
25
+ expect(result.parsed_response).to eql(stubbed_response)
26
+ end
27
+ end
28
+
29
+ describe '#show' do
30
+ let(:params) { { id: 1 } }
31
+ let(:stubbed_response) { { 'id' => 1, 'title' => 'some title' } }
32
+ let(:expected_url) { "https://#{host}/api/quizzes/#{params[:id]}" }
33
+ let(:status_code) { 200 }
34
+
35
+ before do
36
+ stub_request(:get, expected_url)
37
+ .to_return(
38
+ status: status_code,
39
+ body: stubbed_response.to_json,
40
+ headers: { 'Content-Type' => 'application/json' }
41
+ )
42
+ end
43
+
44
+ it 'gets from quiz_api/api/quizzes and returns the response' do
45
+ result = subject.show(params: params, token: 'token')
46
+ expect(result.parsed_response).to eql(stubbed_response)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,42 @@
1
+ describe QuizApiClient::Services::QuizSessionEventsService do
2
+ let(:host) { 'api.host' }
3
+ let(:config) { QuizApiClient::Config.new { |c| c.host = host } }
4
+ let(:subject) { described_class.new(config) }
5
+
6
+ describe '#list' do
7
+ let(:params) { { quiz_session_id: 1 } }
8
+ let(:stubbed_response) { { 'id' => '1' } }
9
+ let(:expected_url) { "https://#{host}/api/quiz_sessions/#{params[:quiz_session_id]}/quiz_session_events" }
10
+ let(:expected_body) { '' }
11
+ let(:status_code) { 200 }
12
+
13
+ before do
14
+ stub_request(:get, expected_url)
15
+ .with(body: expected_body)
16
+ .to_return(
17
+ status: status_code,
18
+ body: stubbed_response.to_json,
19
+ headers: { 'Content-Type' => 'application/json' }
20
+ )
21
+ end
22
+
23
+ it 'posts to quiz_api/api/quizzes and returns the response' do
24
+ response = subject.list(params: params, token: 'token')
25
+ expect(response.parsed_response).to eql(stubbed_response)
26
+ end
27
+
28
+ it 'raises an error when no quiz_session_id is specified' do
29
+ params = {}
30
+ expect do
31
+ subject.list(params: params, token: 'token')
32
+ end.to raise_error 'Quiz Session Id Required'
33
+ end
34
+
35
+ context 'response' do
36
+ it 'returns the response with the correct status code' do
37
+ response = subject.list(params: params, token: 'token')
38
+ expect(response.code).to eq(status_code)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,26 @@
1
+ describe QuizApiClient::Services::QuizSessionResultService do
2
+ let(:host) { 'api.host' }
3
+ let(:config) { QuizApiClient::Config.new { |c| c.host = host } }
4
+ let(:subject) { described_class.new(config) }
5
+
6
+ describe '#show' do
7
+ let(:params) { { id: 1 } }
8
+ let(:stubbed_response) { { 'id' => 1, 'score' => 10 } }
9
+ let(:expected_url) { "https://#{host}/api/results/#{params[:id]}" }
10
+ let(:status_code) { 200 }
11
+
12
+ before do
13
+ stub_request(:get, expected_url)
14
+ .to_return(
15
+ status: status_code,
16
+ body: stubbed_response.to_json,
17
+ headers: { 'Content-Type' => 'application/json' }
18
+ )
19
+ end
20
+
21
+ it 'gets from quiz_api/api/results and returns the response' do
22
+ response = subject.show(params: params, token: 'token')
23
+ expect(response.parsed_response).to eql(stubbed_response)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,49 @@
1
+ describe QuizApiClient::Services::QuizSessionService do
2
+ let(:host) { 'api.host' }
3
+ let(:config) { QuizApiClient::Config.new { |c| c.host = host } }
4
+ let(:subject) { described_class.new(config) }
5
+
6
+ describe '#update' do
7
+ let(:params) { { id: 1, time_limit_seconds: 10 } }
8
+ let(:stubbed_response) { { 'id' => '1' } }
9
+ let(:expected_url) { "https://#{host}/api/quiz_sessions/#{params[:id]}" }
10
+ let(:expected_body) { { quiz_session: { id: 1, time_limit_seconds: 10 } }.to_json }
11
+ let(:status_code) { 200 }
12
+
13
+ before do
14
+ stub_request(:patch, expected_url)
15
+ .with(body: expected_body)
16
+ .to_return(
17
+ status: status_code,
18
+ body: stubbed_response.to_json,
19
+ headers: { 'Content-Type' => 'application/json' }
20
+ )
21
+ end
22
+
23
+ it 'patches to quiz_api/api/quiz_sessions and returns the response' do
24
+ response = subject.update(params: params, token: 'token')
25
+ expect(response.parsed_response).to eql(stubbed_response)
26
+ end
27
+ end
28
+
29
+ describe '#show' do
30
+ let(:params) { { id: 1 } }
31
+ let(:stubbed_response) { { 'id' => 1, 'time_limit_seconds' => 10 } }
32
+ let(:expected_url) { "https://#{host}/api/quiz_sessions/#{params[:id]}" }
33
+ let(:status_code) { 200 }
34
+
35
+ before do
36
+ stub_request(:get, expected_url)
37
+ .to_return(
38
+ status: status_code,
39
+ body: stubbed_response.to_json,
40
+ headers: { 'Content-Type' => 'application/json' }
41
+ )
42
+ end
43
+
44
+ it 'gets from quiz_api/api/quiz_sessions and returns the response' do
45
+ response = subject.show(params: params, token: 'token')
46
+ expect(response.parsed_response).to eql(stubbed_response)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,42 @@
1
+ describe QuizApiClient::Services::QuizSessionsService do
2
+ let(:host) { 'api.host' }
3
+ let(:config) { QuizApiClient::Config.new { |c| c.host = host } }
4
+ let(:subject) { described_class.new(config) }
5
+
6
+ describe '#create' do
7
+ let(:params) { { quiz_id: 1 } }
8
+ let(:stubbed_response) { { 'id' => '1' } }
9
+ let(:expected_url) { "https://#{host}/api/quizzes/#{params[:quiz_id]}/quiz_sessions" }
10
+ let(:expected_body) { { quiz_session: { quiz_id: 1 } }.to_json }
11
+ let(:status_code) { 200 }
12
+
13
+ before do
14
+ stub_request(:post, expected_url)
15
+ .with(body: expected_body)
16
+ .to_return(
17
+ status: status_code,
18
+ body: stubbed_response.to_json,
19
+ headers: { 'Content-Type' => 'application/json' }
20
+ )
21
+ end
22
+
23
+ it 'posts to quiz_api/api/quizzes and returns the response' do
24
+ result = subject.create(params: params, token: 'token')
25
+ expect(result.parsed_response).to eql(stubbed_response)
26
+ end
27
+
28
+ it 'raises an error when no quiz_id is specified' do
29
+ params = {}
30
+ expect do
31
+ subject.create(params: params, token: 'token')
32
+ end.to raise_error 'Quiz Id Required'
33
+ end
34
+
35
+ context 'response' do
36
+ it 'returns the response with the correct status code' do
37
+ response = subject.create(params: params, token: 'token')
38
+ expect(response.code).to eq(status_code)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,71 @@
1
+ describe QuizApiClient::Services::QuizzesService do
2
+ let(:host) { 'api.host' }
3
+ let(:config) { QuizApiClient::Config.new { |c| c.host = host } }
4
+ let(:subject) { described_class.new(config) }
5
+
6
+ describe '#list' do
7
+ let(:params) { { context_id: 1 } }
8
+ let(:stubbed_response) { { 'id' => '1' } }
9
+
10
+ context 'on success' do
11
+ before do
12
+ expected_url = "https://#{host}/api/quizzes?context_id=1"
13
+ stub_request(:get, expected_url)
14
+ .to_return(
15
+ status: 200,
16
+ body: [stubbed_response].to_json,
17
+ headers: { 'Content-Type' => 'application/json' }
18
+ )
19
+ end
20
+
21
+ it 'gets quiz_api/api/quizzes and returns the response' do
22
+ result = subject.list(params: params, token: 'token')
23
+ expect(result.parsed_response[0]).to eql(stubbed_response)
24
+ end
25
+ end
26
+
27
+ context 'on failure' do
28
+ let(:status_code) { 401 }
29
+ before do
30
+ expected_url = "https://#{host}/api/quizzes"
31
+ stub_request(:get, expected_url)
32
+ .to_return(status: status_code)
33
+ end
34
+
35
+ it 'returns the response with the correct code' do
36
+ response = subject.list(params: nil, token: 'token')
37
+ expect(response.code).to eq(status_code)
38
+ end
39
+ end
40
+ end
41
+
42
+ describe '#create' do
43
+ let(:params) { { title: 'Untitled Quiz', owner: 'owner' } }
44
+ let(:stubbed_response) { { 'id' => '1' } }
45
+ let(:expected_url) { "https://#{host}/api/quizzes" }
46
+ let(:expected_body) { { quiz: { title: 'Untitled Quiz', owner: 'owner' } }.to_json }
47
+ let(:status_code) { 200 }
48
+
49
+ before do
50
+ stub_request(:post, expected_url)
51
+ .with(body: expected_body)
52
+ .to_return(
53
+ status: 200,
54
+ body: stubbed_response.to_json,
55
+ headers: { 'Content-Type' => 'application/json' }
56
+ )
57
+ end
58
+
59
+ it 'posts to quiz_api/api/quizzes and returns the response' do
60
+ response = subject.create(params: params, token: 'token')
61
+ expect(response.parsed_response).to eql(stubbed_response)
62
+ end
63
+
64
+ context 'response' do
65
+ it 'returns the response with the correct code' do
66
+ response = subject.create(params: params, token: 'token')
67
+ expect(response.code).to eq(status_code)
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,33 @@
1
+ describe QuizApiClient::Services::SessionItemResultsService do
2
+ let(:host) { 'api.host' }
3
+ let(:config) { QuizApiClient::Config.new { |c| c.host = host } }
4
+ let(:subject) { described_class.new(config) }
5
+
6
+ describe '#index' do
7
+ let(:params) { { quiz_session_id: 1, quiz_session_result_id: 1 } }
8
+ let(:stubbed_response) { { 'id' => '1_1', 'item' => { 'id' => '1' } } }
9
+ let(:expected_url) do
10
+ format(
11
+ 'https://%<host>s/api/quiz_sessions/%<session>d/results/%<result>d/session_item_results',
12
+ host: host,
13
+ session: params[:quiz_session_id],
14
+ result: params[:quiz_session_result_id]
15
+ )
16
+ end
17
+ let(:status_code) { 200 }
18
+
19
+ before do
20
+ stub_request(:get, expected_url)
21
+ .to_return(
22
+ status: status_code,
23
+ body: stubbed_response.to_json,
24
+ headers: { 'Content-Type' => 'application/json' }
25
+ )
26
+ end
27
+
28
+ it 'gets session_item_results and returns the response' do
29
+ response = subject.list(params: params, token: 'token')
30
+ expect(response.parsed_response).to eql(stubbed_response)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,26 @@
1
+ describe QuizApiClient::Services::SessionItemsService do
2
+ let(:host) { 'api.host' }
3
+ let(:config) { QuizApiClient::Config.new { |c| c.host = host } }
4
+ let(:subject) { described_class.new(config) }
5
+
6
+ describe '#index' do
7
+ let(:params) { { quiz_session_id: 1 } }
8
+ let(:stubbed_response) { { 'id' => '1_1', 'item' => { 'id' => '1' } } }
9
+ let(:expected_url) { "https://#{host}/api/quiz_sessions/#{params[:quiz_session_id]}/session_items" }
10
+ let(:status_code) { 200 }
11
+
12
+ before do
13
+ stub_request(:get, expected_url)
14
+ .to_return(
15
+ status: status_code,
16
+ body: stubbed_response.to_json,
17
+ headers: { 'Content-Type' => 'application/json' }
18
+ )
19
+ end
20
+
21
+ it 'gets from quiz_api/api/quiz_sessions/:id/session_items and returns the response' do
22
+ result = subject.list(params: params, token: 'token')
23
+ expect(result.parsed_response).to eql(stubbed_response)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,42 @@
1
+ require 'pact/consumer/rspec'
2
+ require 'pry'
3
+ require 'simplecov'
4
+ require 'webmock/rspec'
5
+
6
+ SimpleCov.start do
7
+ add_filter '/spec/'
8
+ end
9
+
10
+ $LOAD_PATH.unshift File.expand_path('../lib', __dir__)
11
+ require 'quiz_api_client'
12
+
13
+ Dir[File.dirname(__FILE__) + '/support/*.rb'].each { |f| require f }
14
+ Dir[File.dirname(__FILE__) + '/contracts/shared_examples/*.rb'].each { |f| require f }
15
+
16
+ WebMock.disable_net_connect!(
17
+ allow_localhost: true,
18
+ allow: [PactConfig.broker_host]
19
+ )
20
+
21
+ # see https://github.com/realestate-com-au/pact/blob/master/documentation/configuration.md
22
+ Pact.configure do |config|
23
+ config.diff_formatter = :list
24
+ config.logger.level = Logger::DEBUG
25
+ config.pact_dir = 'pacts'
26
+ config.pactfile_write_mode = :overwrite
27
+ config.pactfile_write_order = :chronological
28
+ end
29
+
30
+ Pact.service_consumer PactConfig::QUIZ_API_CLIENT_RUBY do
31
+ has_pact_with PactConfig::Providers::QUIZ_API do
32
+ mock_service :quiz_api do
33
+ port 1234
34
+ pact_specification_version '2.0.0'
35
+ end
36
+ end
37
+ end
38
+
39
+ RSpec.configure do |config|
40
+ config.filter_run_excluding :pact
41
+ config.before { allow_any_instance_of(::Logger).to receive(:info) }
42
+ end
@@ -0,0 +1,64 @@
1
+ module PactConfig
2
+ # These constants ensure we use the correct strings and thus help avoid our
3
+ # accidentally breaking the contract tests
4
+ QUIZ_API_CLIENT_RUBY = 'Quiz API Client Ruby'.freeze
5
+
6
+ # Add new API and LiveEvents providers to this Providers module
7
+ module Providers
8
+ QUIZ_API = 'Quiz API'.freeze
9
+ ALL = Providers.constants.map { |c| Providers.const_get(c) }.freeze
10
+ end
11
+
12
+ class << self
13
+ def pact_uri(pact_path:)
14
+ URI::HTTP.build(
15
+ scheme: protocol,
16
+ userinfo: "#{broker_username}:#{broker_password}",
17
+ host: broker_host,
18
+ path: "/#{pact_path}/#{consumer_tag}"
19
+ ).to_s
20
+ end
21
+
22
+ def broker_uri
23
+ URI::HTTP.build(
24
+ scheme: protocol,
25
+ userinfo: "#{broker_username}:#{broker_password}",
26
+ host: broker_host
27
+ ).to_s
28
+ end
29
+
30
+ def broker_host
31
+ ENV.fetch('PACT_BROKER_HOST', 'pact-broker.docker')
32
+ end
33
+
34
+ def consumer_tag
35
+ ENV.fetch('PACT_CONSUMER_TAG', 'local')
36
+ end
37
+
38
+ def consumer_version
39
+ version = QuizApiClient::VERSION
40
+ sha = ENV['SHA']
41
+ version = "#{version}+#{sha}" if sha
42
+ version
43
+ end
44
+
45
+ def broker_password
46
+ ENV.fetch('PACT_BROKER_PASSWORD', 'broker')
47
+ end
48
+
49
+ def broker_username
50
+ ENV.fetch('PACT_BROKER_USERNAME', 'pact')
51
+ end
52
+
53
+ private
54
+
55
+ def jenkins_build?
56
+ !ENV['JENKINS_URL'].nil?
57
+ end
58
+
59
+ def protocol
60
+ protocol = jenkins_build? ? 'https' : 'http'
61
+ ENV.fetch('PACT_BROKER_PROTOCOL', protocol)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,19 @@
1
+ module PactHelper
2
+ def token_expiration_one_year
3
+ token_expiration_seconds_from_now(seconds: 31_536_000)
4
+ end
5
+
6
+ def token_expiration_seconds_from_now(seconds:)
7
+ Time.now.utc.to_i + seconds
8
+ end
9
+
10
+ def headers(token)
11
+ {
12
+ 'Content-Type' => 'application/json',
13
+ 'AuthType' => 'Signature',
14
+ 'Accept' => 'application/json',
15
+ 'Authorization' => token.to_s,
16
+ 'HOST' => 'localhost:1234'
17
+ }
18
+ end
19
+ end