testrail 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ class TestClass
4
+ extend Testrail::CommandHelper
5
+ end
6
+
7
+ describe Testrail::CommandHelper do
8
+ subject { TestClass }
9
+ let(:key) { '&key=' + String(Testrail.config.api_key) }
10
+ let(:url_prefix) { Testrail.config.server + Testrail.config.api_path}
11
+
12
+ describe ".build_command" do
13
+ context "with nil id" do
14
+ before do
15
+ @command = subject.build_command('a_command')
16
+ end
17
+
18
+ it "creates a valid URL" do
19
+ @command.should eq( url_prefix + 'a_command' + key )
20
+ end
21
+ end
22
+
23
+ context "with a single id" do
24
+ before do
25
+ @command = subject.build_command('a_command', 235)
26
+ end
27
+
28
+ it "creates a valid URL" do
29
+ @command.should eq( url_prefix + 'a_command/235' + key )
30
+ end
31
+ end
32
+
33
+ context "with an array of ids" do
34
+ before do
35
+ @command = subject.build_command('a_command', [235, 'ax45'])
36
+ end
37
+
38
+ it "creates a valid URL" do
39
+ @command.should eq( url_prefix + 'a_command/235/ax45' + key)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,127 @@
1
+ require 'spec_helper'
2
+
3
+ describe Testrail do
4
+ subject { Testrail }
5
+
6
+ describe ".config" do
7
+ it "returns a Testrail::Config object" do
8
+ subject.config.should be_instance_of(Testrail::Config)
9
+ end
10
+ end
11
+
12
+ describe ".configure" do
13
+ context "without a block" do
14
+ before do
15
+ @default_config = subject.config.dup
16
+ @actual_config = subject.configure
17
+ end
18
+
19
+ it "returns the default configuration" do
20
+ @actual_config.server.should eq(@default_config.server)
21
+ @actual_config.api_path.should eq(@default_config.api_path)
22
+ @actual_config.api_key.should eq(@default_config.api_key)
23
+ @actual_config.logger.should be_instance_of(Logger)
24
+ end
25
+ end
26
+
27
+ context "with a block" do
28
+ before do
29
+ @default_config = subject.config.dup
30
+ @new_config = subject.configure { |config|
31
+ config.server = 'localhost'
32
+ }
33
+ end
34
+
35
+ it "overrides specified settings" do
36
+ subject.config.server.should eq(@new_config.server)
37
+ end
38
+
39
+ it "retains any default settings not overwritten" do
40
+ subject.config.api_path.should eq(@default_config.api_path)
41
+ subject.config.api_key.should eq(@default_config.api_key)
42
+ subject.config.logger.should be_instance_of(Logger)
43
+ end
44
+ end
45
+
46
+ describe "@server & @api_path" do
47
+ context "with default settings" do
48
+ it "points to the Cloud Server" do
49
+ subject.config.server.should eq('https://example.testrail.com')
50
+ subject.config.api_path.should eq('/index.php?/miniapi/')
51
+ end
52
+ end
53
+
54
+ context "with custom settings" do
55
+ before do
56
+ @new_server = new_server = 'http://localhost'
57
+ @new_api_path = new_api_path = '/some_path/'
58
+ debugger
59
+ subject.configure do |config|
60
+ config.server = new_server
61
+ config.api_path = new_api_path
62
+ end
63
+ end
64
+
65
+ it "points to the new server" do
66
+ subject.config.server.should eq(@new_server)
67
+ end
68
+
69
+ it "sets the new api_path" do
70
+ subject.config.api_path.should eq(@new_api_path)
71
+ end
72
+ end
73
+ end
74
+
75
+ describe "@api_key" do
76
+ context "with default settings" do
77
+ it "is nil" do
78
+ subject.config.api_key.should be_nil
79
+ end
80
+ end
81
+
82
+ context "with custom settings" do
83
+ before do
84
+ @new_api_key = new_api_key = 'my_new_api_key'
85
+ subject.configure do |config|
86
+ config.api_key = new_api_key
87
+ end
88
+ end
89
+
90
+ it "sets the new api_key" do
91
+ subject.config.api_key.should eq(@new_api_key)
92
+ end
93
+ end
94
+ end
95
+
96
+ describe "@logger" do
97
+ context "with default settings" do
98
+ it "sets the logger to a Logger instance" do
99
+ subject.config.logger.should be_instance_of Logger
100
+ end
101
+
102
+ it "sets the output to STDOUT" do
103
+ Logger.stub!(:new)
104
+ Logger.should_receive(:new).with(STDOUT)
105
+ subject.configure
106
+ end
107
+ end
108
+
109
+ context "with custom settings" do
110
+ before do
111
+ @mock_logger = mock_logger = mock('A Different Logger')
112
+ subject.configure do |config|
113
+ config.logger = mock_logger
114
+ end
115
+ end
116
+
117
+ it "returns the custom log object" do
118
+ subject.config.logger.should eq(@mock_logger)
119
+ end
120
+ end
121
+ end
122
+ end
123
+
124
+ after do
125
+ Testrail.configure
126
+ end
127
+ end
@@ -0,0 +1,172 @@
1
+ require 'spec_helper'
2
+
3
+ describe Testrail::Request do
4
+ subject { Testrail::Request }
5
+
6
+ shared_examples_for "a valid HTTPclient request" do
7
+ describe "interface" do
8
+ context "with successful transactions" do
9
+ before do
10
+ stub_request(:any, url_prefix + key).
11
+ with(action_options).
12
+ to_return(body: JSON.generate({result: true, an_object: 'abc'}))
13
+ stub_request(:any, url_prefix + '/789' + key).
14
+ with(action_options).
15
+ to_return(body: JSON.generate({result: true, an_object: 'abc'}))
16
+ stub_request(:any, url_prefix + '/789/a4g8' + key).
17
+ with(action_options).
18
+ to_return(body: JSON.generate({result: true, an_object: 'abc'}))
19
+ end
20
+
21
+ it "returns a Testrail::Response object" do
22
+ result = subject.send(method, command, action_options)
23
+ result.should be_instance_of Testrail::Response
24
+ end
25
+
26
+ context "with no ids" do
27
+ it "should call the HTTPclient with the correct parameters" do
28
+ subject.send(method, command, action_options)
29
+ WebMock.should have_requested(method, url_prefix + key)
30
+ end
31
+ end
32
+
33
+ context "with a single id" do
34
+ it "should call the HTTPclient with the correct parameters" do
35
+ subject.send(method, command, 789, action_options)
36
+ WebMock.should have_requested(method, url_prefix + '/789' + key)
37
+ end
38
+ end
39
+
40
+ context "with multiple ids" do
41
+ it "should call the HTTPclient with the correct parameters" do
42
+ subject.send(method, command, [789, 'a4g8'], action_options)
43
+ WebMock.should have_requested(method, url_prefix + '/789/a4g8' + key)
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ describe 'error handling' do
50
+ context "with http_error response" do
51
+ before do
52
+ stub_request(:any, server + 'return_http_response_error' + key).
53
+ with(action_options).
54
+ to_return(status: 500)
55
+ @result = subject.send(method, 'return_http_response_error', action_options)
56
+ end
57
+
58
+ it "returns a Testrail::Response object" do
59
+ @result.should be_instance_of Testrail::Response
60
+ end
61
+
62
+ it "returns a resulting object with @success set to false" do
63
+ @result.success.should be_false
64
+ end
65
+
66
+ it "puts the HTTP error type in @error" do
67
+ @result.error.should eq("InternalServerError")
68
+ end
69
+ end
70
+
71
+ context "with a timeout" do
72
+ before do
73
+ @timeout_command = 'http_timeout'
74
+ @expected_url = server + @timeout_command + key
75
+ @logger = mock('Logger', error: 'an error')
76
+ Testrail.stub!(:logger).and_return(@logger)
77
+ stub_request(:any, @expected_url).
78
+ with(action_options).
79
+ to_timeout
80
+ end
81
+
82
+ it "raises a Timeout::Error" do
83
+ lambda {
84
+ @result = subject.send(method, @timeout_command, action_options)
85
+ }.should raise_error(Timeout::Error)
86
+ end
87
+
88
+ context "with a configured logger" do
89
+ it "logs the error" do
90
+ @logger.should_receive(:error).with("Timeout connecting to #{method.to_s.upcase} #{@expected_url}")
91
+ @error = subject.send(method, @timeout_command, action_options) rescue $!
92
+ end
93
+ end
94
+
95
+ context "with a nil logger" do
96
+ before do
97
+ Testrail.configure do |config|
98
+ config.logger = nil
99
+ end
100
+ end
101
+
102
+ it "does not log the error" do
103
+ Testrail.logger.should_not_receive(:error)
104
+ end
105
+
106
+ after do
107
+ Testrail.configure
108
+ end
109
+ end
110
+ end
111
+
112
+ context "with any other exception" do
113
+ before do
114
+ @exception_command = 'throw_exception'
115
+ @expected_url = server + @exception_command + key
116
+ @logger = mock('Logger', error: 'an error')
117
+ Testrail.stub!(:logger).and_return(@logger)
118
+ stub_request(:any, @expected_url).
119
+ with(action_options).
120
+ to_raise(StandardError.new('unexpected exception'))
121
+ end
122
+
123
+ it "raises the exception" do
124
+ lambda {
125
+ @result = subject.send(method, @exception_command, action_options)
126
+ }.should raise_error(StandardError, 'unexpected exception')
127
+ end
128
+
129
+ context "with a configured logger" do
130
+ it "logs the exception" do
131
+ @logger.should_receive(:error).with(/Unexpected exception/)
132
+ @error = subject.send(method, @exception_command, action_options) rescue $!
133
+ end
134
+ end
135
+
136
+ context "with a nil logger" do
137
+ before do
138
+ Testrail.configure do |config|
139
+ config.logger = nil
140
+ end
141
+ end
142
+
143
+ it "does not log the error" do
144
+ Testrail.logger.should_not_receive(:error)
145
+ end
146
+
147
+ after do
148
+ Testrail.configure
149
+ end
150
+ end
151
+ end
152
+ end
153
+ end
154
+
155
+ describe ".get" do
156
+ describe "shared behaviors" do
157
+ it_should_behave_like "a valid HTTPclient request" do
158
+ let(:method) { :get }
159
+ let(:action_options) { get_options }
160
+ end
161
+ end
162
+ end
163
+
164
+ describe ".post" do
165
+ describe "shared behaviors" do
166
+ it_should_behave_like "a valid HTTPclient request" do
167
+ let(:method) { :post }
168
+ let(:action_options) { post_options }
169
+ end
170
+ end
171
+ end
172
+ end
@@ -0,0 +1,119 @@
1
+ require 'spec_helper'
2
+
3
+ class TestResponse
4
+ attr_accessor :body, :code
5
+ def initialize(code, opts = nil)
6
+ @code = code
7
+ unless opts.nil?
8
+ @body = JSON.generate(opts) rescue opts
9
+ end
10
+ end
11
+ end
12
+
13
+ describe Testrail::Response do
14
+ subject { Testrail::Response }
15
+
16
+ describe "initialize" do
17
+ context "with unsuccessful server response" do
18
+ before do
19
+ @response1 = subject.new(TestResponse.new('500'))
20
+ @response2 = subject.new(TestResponse.new('404'))
21
+ @response3 = subject.new(TestResponse.new('401'))
22
+ @response4 = subject.new(TestResponse.new('403'))
23
+ end
24
+
25
+ it "assigns false to @success" do
26
+ @response1.success.should be_false
27
+ @response2.success.should be_false
28
+ @response3.success.should be_false
29
+ @response4.success.should be_false
30
+ end
31
+
32
+ it "assigns the code definition to @error" do
33
+ @response1.error.should eq('InternalServerError')
34
+ @response2.error.should eq('NotFound')
35
+ @response3.error.should eq('Unauthorized')
36
+ @response4.error.should eq('Forbidden')
37
+ end
38
+
39
+ it "does not assign anything to @payload" do
40
+ @response1.payload.should be_nil
41
+ @response2.payload.should be_nil
42
+ @response3.payload.should be_nil
43
+ @response4.payload.should be_nil
44
+ end
45
+ end
46
+
47
+ context "with successful response, but no body" do
48
+ before do
49
+ @response = subject.new(TestResponse.new(200))
50
+ end
51
+
52
+ it "does not assign anything to @success" do
53
+ @response.success.should be_nil
54
+ end
55
+
56
+ it "does not assign anything to @error" do
57
+ @response.error.should be_nil
58
+ end
59
+
60
+ it "does not assign anything to @payload" do
61
+ @response.payload.should be_nil
62
+ end
63
+ end
64
+
65
+ context "with successful response, including a body" do
66
+ before do
67
+ @response = subject.new(TestResponse.new(200, result: true, some_obj: {id: 1, msg: 'some_msg'}))
68
+ end
69
+
70
+ it "assigns true to @success" do
71
+ @response.success.should be_true
72
+ end
73
+
74
+ it "does not assign anything to @error" do
75
+ @response.error.should be_nil
76
+ end
77
+
78
+ it "assigns the response body object to @payload" do
79
+ @response.payload.should eq({"some_obj" => {"id" => 1, "msg" => 'some_msg'}})
80
+ end
81
+ end
82
+
83
+ context "with the server successfully sending back an error" do
84
+ before do
85
+ @response = subject.new(TestResponse.new(200, result: false, error: 'Some error'))
86
+ end
87
+
88
+ it "assigns false to @success" do
89
+ @response.success.should be_false
90
+ end
91
+
92
+ it "assigns the returned error message to @error" do
93
+ @response.error.should eq('Some error')
94
+ end
95
+
96
+ it "does not assign anything to @payload" do
97
+ @response.payload.should be_nil
98
+ end
99
+ end
100
+
101
+ context "with the server returning an invalid JSON object" do
102
+ before do
103
+ @response = subject.new(TestResponse.new(200, 'something_with_malformed_response'))
104
+ end
105
+
106
+ it "assigns false to @success" do
107
+ @response.success.should be_false
108
+ end
109
+
110
+ it "assigns a Malformed JSON error message to @error" do
111
+ @response.error.should =~ /Malformed JSON response/
112
+ end
113
+
114
+ it "does not assign anything to @payload" do
115
+ @response.payload.should be_nil
116
+ end
117
+ end
118
+ end
119
+ end