cm_quiz 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,68 @@
1
+ require 'json'
2
+
3
+ module CmQuiz
4
+ class ReviewQuiz
5
+ def initialize(endpoint)
6
+ @endpoint = endpoint
7
+ end
8
+
9
+ def perform
10
+ test_results = perform_tests(@endpoint)
11
+ build_message(test_results, @endpoint)
12
+ end
13
+
14
+ private
15
+
16
+ def perform_tests(endpoint)
17
+ project_api = ProjectAPI.new(endpoint)
18
+ tests = []
19
+ tests << Review::LoginUser.new(project_api: project_api).perform
20
+ tests << Review::SignUpUser.new(project_api: project_api).perform
21
+ tests << Review::GetUserInfo.new(project_api: project_api).perform
22
+ tests << Review::CreateIdea.new(project_api: project_api).perform
23
+ tests << Review::GetIdeas.new(project_api: project_api).perform
24
+ tests << Review::UpdateIdea.new(project_api: project_api).perform
25
+ tests << Review::DeleteIdea.new(project_api: project_api).perform
26
+ tests
27
+ end
28
+
29
+ def build_message(test_results, endpoint)
30
+ score, failed_results = arrange_results(test_results)
31
+ example_messages = failed_results.map do |result|
32
+ # m = (result[0].to_s + ": " + result[2].to_s)
33
+ verb = result[0][:verb].upcase
34
+ path = result[0][:path]
35
+ options = result[0][:options]
36
+ messages = ["#{verb} #{@endpoint}#{path}"]
37
+ messages << ""
38
+ messages << "Request options:"
39
+ messages << ""
40
+ messages << JSON.pretty_generate(options)
41
+ messages << ""
42
+ messages << "Error message:"
43
+ messages << ""
44
+ error_message = result[2].to_s
45
+ error_message = error_message.truncate(500) + '...' if error_message.size > 500
46
+ messages << error_message
47
+
48
+ messages.join("\n")
49
+ end
50
+ text = ([
51
+ "endpoint: #{endpoint}",
52
+ "score: #{score}",
53
+ "#{test_results.size} examples, #{failed_results.size} failures",
54
+ ""
55
+ ] + example_messages).join("\n")
56
+ text
57
+ end
58
+
59
+ def arrange_results(test_results)
60
+ total_result_size = test_results.size
61
+ failed_results = test_results.select { |test_result| test_result[1] == false }
62
+ failed_result_size = failed_results.size
63
+ score = 100 * (total_result_size - failed_result_size)/ total_result_size.to_f
64
+
65
+ [score.round(2), failed_results]
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,3 @@
1
+ module CmQuiz
2
+ VERSION = '0.0.1'
3
+ end
data/lib/cm_quiz.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'cm_quiz/cli'
2
+ require 'cm_quiz/project_api'
3
+ require 'cm_quiz/review/base_review'
4
+ require 'cm_quiz/review/sign_up_user'
5
+ require 'cm_quiz/review/login_user'
6
+ require 'cm_quiz/review/get_user_info'
7
+ require 'cm_quiz/review/create_idea'
8
+ require 'cm_quiz/review/get_ideas'
9
+ require 'cm_quiz/review/update_idea'
10
+ require 'cm_quiz/review/delete_idea'
11
+ require 'cm_quiz/factory/user'
12
+ require 'cm_quiz/factory/idea'
13
+ require 'cm_quiz/review_quiz'
14
+
15
+ require 'cm_quiz/version'
16
+
17
+ module CmQuiz
18
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe CmQuiz::Factory::Idea do
4
+ describe '#create' do
5
+ let(:jwt) { 'jwt' }
6
+ let(:mock_idea) do
7
+ {
8
+ content: 'the-content',
9
+ impact: 7,
10
+ ease: 8,
11
+ confidence: 9,
12
+ average_score: 8.0
13
+ }
14
+ end
15
+ let!(:project_api) do
16
+ api = double
17
+ allow(api).to receive(:request) do |verb, path, opts|
18
+ double(success?: true, body: mock_idea.to_json, code: 200)
19
+ end
20
+ api
21
+ end
22
+ let(:factory) do
23
+ CmQuiz::Factory::Idea.new({
24
+ project_api: project_api,
25
+ jwt: jwt
26
+ })
27
+ end
28
+ before :each do
29
+ user_factory = double(create: [jwt, 'refresh_token'])
30
+ allow(CmQuiz::Factory::User).to receive(:new).and_return(user_factory)
31
+ end
32
+
33
+ it "should create idea" do
34
+ res = factory.create
35
+
36
+ expect(res['content']).to eq(mock_idea[:content])
37
+ expect(res['impact']).to eq(mock_idea[:impact])
38
+ expect(res['ease']).to eq(mock_idea[:ease])
39
+ options = {
40
+ headers: {
41
+ 'x-access-token' => jwt
42
+ },
43
+ body: {
44
+ content: mock_idea[:content],
45
+ impact: mock_idea[:impact],
46
+ ease: mock_idea[:ease],
47
+ confidence: mock_idea[:confidence]
48
+ }
49
+ }
50
+ args = [:post, '/ideas', options]
51
+ expect(project_api).to have_received(:request).with(*args)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe CmQuiz::Factory::User do
4
+ describe '#create' do
5
+ let(:jwt) { 'jwt' }
6
+ let(:refresh_token) { 'refresh_token' }
7
+ let!(:project_api) do
8
+ api = double
9
+ allow(api).to receive(:request) do |verb, path, opts|
10
+ double(success?: true, body: { jwt: jwt, refresh_token: refresh_token}.to_json, code: 200)
11
+ end
12
+ api
13
+ end
14
+ let(:factory) do
15
+ CmQuiz::Factory::User.new({
16
+ project_api: project_api
17
+ })
18
+ end
19
+
20
+ it "should create user" do
21
+ res = factory.create
22
+
23
+ expect(res[0]).to eq(jwt)
24
+ expect(res[1]).to eq(refresh_token)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'ProjectAPI' do
4
+ describe '#request' do
5
+ let(:endpoint) { "https://my-awesome-api.com" }
6
+ let(:verb) { :post }
7
+ let(:path) { '/greeting' }
8
+ let(:options) do
9
+ {
10
+ body: { foo: 'bar' },
11
+ headers: { 'X-Access-Token' => 'secret-token' }
12
+ }
13
+ end
14
+ let(:api) do
15
+ api = CmQuiz::ProjectAPI.new(endpoint)
16
+ end
17
+
18
+ it "should request clearbit" do
19
+ stub_request(verb, "#{endpoint}#{path}")
20
+
21
+ api.request(verb, path, options)
22
+
23
+ assert_requested(verb, "#{endpoint}#{path}") do |req|
24
+ body = JSON.parse(req.body)
25
+ expect(body['foo']).to eq('bar')
26
+ expect(req.headers['X-Access-Token']).to eq(options[:headers]['X-Access-Token'])
27
+ end
28
+ end
29
+
30
+ context "when response is not success" do
31
+ it "should raise exception" do
32
+ res_body = 'error_message'
33
+ stub_request(verb, "#{endpoint}#{path}").to_return(status: 404, body: res_body)
34
+
35
+ expect {
36
+ api.request(verb, path, options)
37
+ }.to raise_error(CmQuiz::ProjectAPI::PerformFailed)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe CmQuiz::Review::CreateIdea do
4
+ describe '#perform' do
5
+ let(:mock_idea) do
6
+ {
7
+ content: 'test-content',
8
+ impact: 7,
9
+ ease: 8,
10
+ confidence: 9,
11
+ average_score: 8.0
12
+ }
13
+ end
14
+ let!(:project_api) do
15
+ api = double
16
+ allow(api).to receive(:request) do |verb, path, opts|
17
+ double(success?: true, body: mock_idea.to_json, code: 200)
18
+ end
19
+ api
20
+ end
21
+ let(:service) do
22
+ CmQuiz::Review::CreateIdea.new({
23
+ project_api: project_api
24
+ })
25
+ end
26
+ before :each do
27
+ factory = double(create: ['jwt', 'refresh_token'])
28
+ allow(CmQuiz::Factory::User).to receive(:new).and_return(factory)
29
+ end
30
+
31
+ it "should pass test" do
32
+ test_result = service.perform
33
+
34
+ expect(test_result).to eq(["post /ideas", true, nil])
35
+ options = {
36
+ headers: {
37
+ 'x-access-token' => 'jwt'
38
+ },
39
+ body: {
40
+ content: mock_idea[:content],
41
+ impact: mock_idea[:impact],
42
+ ease: mock_idea[:ease],
43
+ confidence: mock_idea[:confidence]
44
+ }
45
+ }
46
+ args = [:post, '/ideas', options]
47
+ expect(project_api).to have_received(:request).with(*args)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe CmQuiz::Review::DeleteIdea do
4
+ describe '#perform' do
5
+ let!(:project_api) do
6
+ api = double
7
+ allow(api).to receive(:request) do |verb, path, opts|
8
+ if path == '/ideas'
9
+ double(success?: true, body: [].to_json, code: 200)
10
+ else
11
+ double(success?: true, body: "", code: 200)
12
+ end
13
+ end
14
+ api
15
+ end
16
+ let(:idea_id) { 1 }
17
+ let(:mock_idea_payload) do
18
+ {
19
+ 'id' => idea_id
20
+ }
21
+ end
22
+ let(:service) do
23
+ CmQuiz::Review::DeleteIdea.new({
24
+ project_api: project_api
25
+ })
26
+ end
27
+ before :each do
28
+ user_factory = double(create: ['jwt', 'refresh_token'])
29
+ allow(CmQuiz::Factory::User).to receive(:new).and_return(user_factory)
30
+ idea_factory = double(create: mock_idea_payload)
31
+ allow(CmQuiz::Factory::Idea).to receive(:new).and_return(idea_factory)
32
+ end
33
+
34
+ it "should pass test" do
35
+ test_result = service.perform
36
+
37
+ expect(test_result).to eq(["delete /ideas/:idea_id", true, nil])
38
+ options = {
39
+ headers: {
40
+ 'x-access-token' => 'jwt'
41
+ }
42
+ }
43
+ args = [:delete, "/ideas/#{idea_id}", options]
44
+ expect(project_api).to have_received(:request).with(*args)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+ require 'date'
3
+
4
+ RSpec.describe CmQuiz::Review::GetIdeas do
5
+ describe '#perform' do
6
+ let!(:project_api) do
7
+ api = double
8
+ allow(api).to receive(:request) do |verb, path, opts|
9
+ double(success?: true, body: mock_ideas_payload.to_json, code: 200)
10
+ end
11
+ api
12
+ end
13
+ let(:idea_id) { 1 }
14
+ let(:now) { Time.now }
15
+ let(:mock_ideas_payload) do
16
+ 3.times.map do |i|
17
+ {
18
+ 'id' => i,
19
+ 'content' => 'test-content',
20
+ 'impact' => 7,
21
+ 'ease' => 8,
22
+ 'confidence' => 9,
23
+ 'average_score' => 8.0,
24
+ 'created_at' => now.to_i
25
+ }
26
+ end
27
+ end
28
+ let(:idea_factory) do
29
+ factory = double
30
+ call_time = 0
31
+ allow(factory).to receive(:create) do
32
+ idea = mock_ideas_payload[call_time]
33
+ call_time += 1
34
+ idea
35
+ end
36
+ factory
37
+ end
38
+ let(:service) do
39
+ CmQuiz::Review::GetIdeas.new({
40
+ project_api: project_api
41
+ })
42
+ end
43
+ before :each do
44
+ user_factory = double(create: ['jwt', 'refresh_token'])
45
+ allow(CmQuiz::Factory::User).to receive(:new).and_return(user_factory)
46
+ allow(CmQuiz::Factory::Idea).to receive(:new).and_return(idea_factory)
47
+ end
48
+
49
+ it "should pass test" do
50
+ test_result = service.perform
51
+
52
+ expect(test_result).to eq(["get /ideas", true, nil])
53
+ options = {
54
+ headers: {
55
+ 'x-access-token' => 'jwt'
56
+ },
57
+ query: {
58
+ page: 1
59
+ }
60
+ }
61
+ args = [:get, "/ideas", options]
62
+ expect(project_api).to have_received(:request).with(*args)
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe CmQuiz::Review::GetUserInfo do
4
+ describe '#perform' do
5
+ let!(:project_api) do
6
+ api = double
7
+ allow(api).to receive(:request) do |verb, path, opts|
8
+ double(success?: true, body: { email: email, name: name}.to_json, code: 200)
9
+ end
10
+ api
11
+ end
12
+ let(:hex) { '58714e1256' }
13
+ let(:name) { "codementor-test-#{hex}" }
14
+ let(:email) { "#{name}@codementor.io"}
15
+ let(:service) do
16
+ CmQuiz::Review::GetUserInfo.new({
17
+ project_api: project_api
18
+ })
19
+ end
20
+ before :each do
21
+ factory = double(create: ['jwt', 'refresh_token'])
22
+ allow(CmQuiz::Factory::User).to receive(:new).and_return(factory)
23
+ allow(SecureRandom).to receive(:hex).and_return(hex)
24
+ end
25
+
26
+ it "should pass test" do
27
+ test_result = service.perform
28
+
29
+ expect(test_result).to eq(["get /me", true, nil])
30
+ options = {
31
+ headers: {
32
+ 'x-access-token' => 'jwt'
33
+ }
34
+ }
35
+ args = [:get, '/me', options]
36
+ expect(project_api).to have_received(:request).with(*args)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe CmQuiz::Review::LoginUser do
4
+ describe '#perform' do
5
+ let!(:project_api) do
6
+ api = double
7
+ allow(api).to receive(:request) do |verb, path, opts|
8
+ double(success?: true, body: { jwt: '', refresh_token: ''}.to_json, code: 200)
9
+ end
10
+ api
11
+ end
12
+ let(:service) do
13
+ CmQuiz::Review::LoginUser.new({
14
+ project_api: project_api
15
+ })
16
+ end
17
+
18
+ it "should pass test" do
19
+ test_result = service.perform
20
+
21
+ expect(test_result).to eq(["post /access-tokens", true, nil])
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe CmQuiz::Review::SignUpUser do
4
+ describe '#perform' do
5
+ let!(:project_api) do
6
+ api = double
7
+ allow(api).to receive(:request) do |verb, path, opts|
8
+ double(success?: true, body: { jwt: '', refresh_token: ''}.to_json, code: 200)
9
+ end
10
+ api
11
+ end
12
+ let(:service) do
13
+ CmQuiz::Review::SignUpUser.new({
14
+ project_api: project_api
15
+ })
16
+ end
17
+
18
+ it "should pass test" do
19
+ test_result = service.perform
20
+
21
+ expect(test_result).to eq(["post /users", true, nil])
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe CmQuiz::Review::UpdateIdea do
4
+ describe '#perform' do
5
+ let(:mock_idea) do
6
+ {
7
+ content: 'test-new-content',
8
+ impact: 6,
9
+ ease: 7,
10
+ confidence: 8,
11
+ average_score: 7.0
12
+ }
13
+ end
14
+ let!(:project_api) do
15
+ api = double
16
+ allow(api).to receive(:request) do |verb, path, opts|
17
+ double(success?: true, body: mock_idea.to_json, code: 200)
18
+ end
19
+ api
20
+ end
21
+ let(:idea_id) { 1 }
22
+ let(:mock_idea_payload) do
23
+ {
24
+ 'id' => idea_id
25
+ }
26
+ end
27
+ let(:service) do
28
+ CmQuiz::Review::UpdateIdea.new({
29
+ project_api: project_api
30
+ })
31
+ end
32
+ before :each do
33
+ user_factory = double(create: ['jwt', 'refresh_token'])
34
+ allow(CmQuiz::Factory::User).to receive(:new).and_return(user_factory)
35
+ idea_factory = double(create: mock_idea_payload)
36
+ allow(CmQuiz::Factory::Idea).to receive(:new).and_return(idea_factory)
37
+ end
38
+
39
+ it "should pass test" do
40
+ test_result = service.perform
41
+
42
+ expect(test_result).to eq(["put /ideas/:idea_id", true, nil])
43
+ options = {
44
+ headers: {
45
+ 'x-access-token' => 'jwt'
46
+ },
47
+ body: {
48
+ content: mock_idea[:content],
49
+ impact: mock_idea[:impact],
50
+ ease: mock_idea[:ease],
51
+ confidence: mock_idea[:confidence]
52
+ }
53
+ }
54
+ args = [:put, "/ideas/#{idea_id}", options]
55
+ expect(project_api).to have_received(:request).with(*args)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,7 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
3
+
4
+ require 'rubygems'
5
+ require 'webmock/rspec'
6
+ require 'cm_quiz'
7
+ require 'pry'
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cm_quiz
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - ben
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.19.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.19.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: httparty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.15.6
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.15.6
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Review your codementor quiz1
70
+ email: ben@codementor.ios
71
+ executables:
72
+ - cm-quiz
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".rspec"
77
+ - Gemfile
78
+ - Gemfile.lock
79
+ - README.md
80
+ - bin/cm-quiz
81
+ - cm_quiz.gemspec
82
+ - lib/cm_quiz.rb
83
+ - lib/cm_quiz/cli.rb
84
+ - lib/cm_quiz/factory/idea.rb
85
+ - lib/cm_quiz/factory/user.rb
86
+ - lib/cm_quiz/project_api.rb
87
+ - lib/cm_quiz/review/base_review.rb
88
+ - lib/cm_quiz/review/create_idea.rb
89
+ - lib/cm_quiz/review/delete_idea.rb
90
+ - lib/cm_quiz/review/get_ideas.rb
91
+ - lib/cm_quiz/review/get_user_info.rb
92
+ - lib/cm_quiz/review/login_user.rb
93
+ - lib/cm_quiz/review/sign_up_user.rb
94
+ - lib/cm_quiz/review/update_idea.rb
95
+ - lib/cm_quiz/review_helper.rb
96
+ - lib/cm_quiz/review_quiz.rb
97
+ - lib/cm_quiz/version.rb
98
+ - spec/lib/cm_quiz/factory/idea_spec.rb
99
+ - spec/lib/cm_quiz/factory/user_spec.rb
100
+ - spec/lib/cm_quiz/project_api_spec.rb
101
+ - spec/lib/cm_quiz/review/create_idea_spec.rb
102
+ - spec/lib/cm_quiz/review/delete_idea_spec.rb
103
+ - spec/lib/cm_quiz/review/get_ideas_spec.rb
104
+ - spec/lib/cm_quiz/review/get_user_info_spec.rb
105
+ - spec/lib/cm_quiz/review/login_user_spec.rb
106
+ - spec/lib/cm_quiz/review/sign_up_user_spec.rb
107
+ - spec/lib/cm_quiz/review/update_idea_spec.rb
108
+ - spec/spec_helper.rb
109
+ homepage: https://github.com/codementordev/cm_quiz
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '2.3'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.6.12
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Review your codementor quiz
133
+ test_files: []