clavem 1.4.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,52 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the clavem gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ module Clavem
8
+ # A class to handle oAuth callbacks on the browser via HTTP.
9
+ class Server < EM::Connection
10
+ include EM::HttpServer
11
+
12
+ # The template to send to the browser.
13
+ TEMPLATE = <<-EOTEMPLATE
14
+ <html>
15
+ <head>
16
+ <title>Clavem</title>
17
+ <script type="text/javascript"> close_window = (function(){ window.open("", "_self", ""); window.close(); })(); </script>
18
+ </head>
19
+ <body><h4>%s</h4></body>
20
+ </html>
21
+ EOTEMPLATE
22
+
23
+ # Creates a new server.
24
+ #
25
+ # @param authorizer [Authorizer] The authorizer of this server.
26
+ def initialize(authorizer)
27
+ @authorizer = authorizer
28
+ end
29
+
30
+ # Save the token and sends a response back to the user.
31
+ def process_http_request
32
+ # Handle the token
33
+ token = @authorizer.response_handler.call(CGI::parse(@http_query_string))
34
+ if token then
35
+ @authorizer.token = token
36
+ @authorizer.status = :succeeded
37
+ else
38
+ @authorizer.status = :denied
39
+ end
40
+
41
+ # Build the request
42
+ response = EM::DelegatedHttpResponse.new(self)
43
+ response.status = 200
44
+ response.content_type("text/html")
45
+ response.content = TEMPLATE % [@authorizer.i18n.template]
46
+ response.send_response
47
+
48
+ # Stop after serving the request.
49
+ EM.add_timer(0.1) { EM.stop }
50
+ end
51
+ end
52
+ end
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
  #
3
- # This file is part of the clavem gem. Copyright (C) 2013 and above Shogun <shogun_panda@me.com>.
3
+ # This file is part of the clavem gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
4
4
  # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
5
  #
6
6
 
@@ -10,10 +10,10 @@ module Clavem
10
10
  # @see http://semver.org
11
11
  module Version
12
12
  # The major version.
13
- MAJOR = 1
13
+ MAJOR = 2
14
14
 
15
15
  # The minor version.
16
- MINOR = 4
16
+ MINOR = 0
17
17
 
18
18
  # The patch version.
19
19
  PATCH = 0
@@ -1,16 +1,25 @@
1
1
  # encoding: utf-8
2
2
  #
3
- # This file is part of the clavem gem. Copyright (C) 2013 and above Shogun <shogun_panda@me.com>.
3
+ # This file is part of the clavem gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
4
4
  # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
5
  #
6
6
 
7
7
  ---
8
8
  clavem:
9
- default_title: "Clavem Authorization"
10
9
  errors:
11
10
  response_failure: "Cannot handle response: %1"
12
11
  open_failure: "Cannot open URL %1: %2"
13
- template:
14
- header_success: "Authentication succeeded!"
15
- header_failure: "Authentication failed!"
16
- close_message: "This window should close immediately. Alternatively you can close it by <a href=\"javascript:close_window();\">clicking here</a>."
12
+ template: "This window will close in few moments. If this doesn't happens you can close it by yourself, this won't cause any problem."
13
+ application_description: "A local callback server for oAuth web-flow."
14
+ application_help_url: "The URL where to send the user to start authorization."
15
+ application_help_host: "The host address on which listening for the reply. Default is localhost."
16
+ application_help_port: "The port on which listening for the reply. Default is 777`."
17
+ application_help_command: "The command to open the URL. {{URL} is replaced with the specified URL. Default is 'open \"{{URL}}\"'."
18
+ application_help_timeout: "The amount of seconds to wait for response from the remote endpoint before returning a failure."
19
+ application_help_quiet: "Do not print anything but errors."
20
+ application_meta_port: "PORT"
21
+ application_meta_command: "COMMAND"
22
+ cli_running: "Clavem is waiting for a response at URL {mark=bright}%1{/mark} ..."
23
+ cli_obtaining: "Obtaining token from URL {mark=bright}%1{/mark}."
24
+ cli_succeeded: "Obtained access token is: {mark=bright}%1{/mark}"
25
+ cli_failed: "No access token obtained."
@@ -1,16 +1,25 @@
1
1
  # encoding: utf-8
2
2
  #
3
- # This file is part of the clavem gem. Copyright (C) 2013 and above Shogun <shogun_panda@me.com>.
3
+ # This file is part of the clavem gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
4
4
  # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
5
  #
6
6
 
7
7
  ---
8
8
  clavem:
9
- default_title: Autorizzazione Clavem
10
9
  errors:
11
10
  response_failure: "Impossibile gestire una risposta: %1"
12
11
  open_failure: "Impossible aprire l'URL %1: %2"
13
- template:
14
- header_success: "Autenticazione riuscita!"
15
- header_failure: "Autenticazione fallita!"
16
- close_message: "Questa finestra dovrebbe chiudersi immediatamente. Alternativamente puoi chiuderla <a href=\"javascript:close_window();\">cliccando qui</a>."
12
+ template: "Questa finestra si chiuderà in pochi momenti. Se non succede puoi chiuderla da solo, ciò non causerà alcun problema."
13
+ application_description: "Un server callback locale per il web-flow oAuth."
14
+ application_help_url: "L'URL dove mandare l'utente per iniziare l'autenticazione."
15
+ application_help_host: "L'indirizzo host dove rimanere in ascolto per la risposta. Il default è localhost."
16
+ application_help_port: "La porta dove rimanere in ascolto per la risposta. La porta di default è 7772."
17
+ application_help_command: "Il comando per aprire l'URL. {{URL}} è sostituito con l'URL specificato. Il default è 'open \"{{URL}}\"'."
18
+ application_help_timeout: "Il numero di secondi da attendere per la risposta dall'endpoint remoto prima di ritornare un fallimento."
19
+ application_help_quiet: "Non mostrare nulla tranne che gli errori."
20
+ application_meta_port: "PORTA"
21
+ application_meta_command: "COMANDO"
22
+ cli_running: "Clavem è in attesa di una risposta all'URL {mark=bright}%1{/mark} ..."
23
+ cli_obtaining: "Ottengo il token dall'URL {mark=bright}%1{/mark}."
24
+ cli_succeeded: "Il token di accesso ottenuto è: {mark=bright}%1{/mark}"
25
+ cli_failed: "Nessun token di accesso ottenuto."
@@ -1,47 +1,18 @@
1
1
  # encoding: utf-8
2
2
  #
3
- # This file is part of the clavem gem. Copyright (C) 2013 and above Shogun <shogun_panda@me.com>.
3
+ # This file is part of the clavem gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
4
4
  # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
5
  #
6
6
 
7
7
  require "spec_helper"
8
8
 
9
9
  describe Clavem::Authorizer do
10
- class ClavemDummyServer
11
- attr_accessor :started
12
-
13
- def start
14
- self.started = true
15
- end
16
-
17
- def shutdown
18
- self.started = false
19
- end
20
-
21
- def mount_proc(path, &handler)
22
-
23
- end
24
- end
25
-
26
- class ClavemDummyRequest
27
- attr_reader :query
28
-
29
- def initialize(token = nil)
30
- @query = {"oauth_token" => token}
31
- end
32
- end
33
-
34
- class ClavemDummyResponse
35
- attr_accessor :status
36
- attr_accessor :body
37
- end
38
-
39
- let(:instance){::Clavem::Authorizer.new}
10
+ let(:subject){::Clavem::Authorizer.new}
40
11
 
41
12
  describe ".instance" do
42
13
  it "should call .new with the passed arguments" do
43
- expect(::Clavem::Authorizer).to receive(:new).with("HOST", "PORT", "COMMAND", "TITLE", "TEMPLATE", "TIMEOUT")
44
- ::Clavem::Authorizer.instance("HOST", "PORT", "COMMAND", "TITLE", "TEMPLATE", "TIMEOUT")
14
+ expect(::Clavem::Authorizer).to receive(:new).with("HOST", "PORT", "COMMAND", "TIMEOUT")
15
+ ::Clavem::Authorizer.instance("HOST", "PORT", "COMMAND", "TIMEOUT")
45
16
  end
46
17
 
47
18
  it "should return the same instance" do
@@ -53,7 +24,7 @@ describe Clavem::Authorizer do
53
24
  it "should return a new instance if requested to" do
54
25
  allow(::Clavem::Authorizer).to receive(:new) { Time.now }
55
26
  authorizer = ::Clavem::Authorizer.instance("FIRST")
56
- expect(::Clavem::Authorizer.instance("HOST", "PORT", "COMMAND", "TITLE", "TEMPLATE", "TIMEOUT", true)).not_to be(authorizer)
27
+ expect(::Clavem::Authorizer.instance("HOST", "PORT", "COMMAND", "TIMEOUT", true)).not_to be(authorizer)
57
28
  end
58
29
  end
59
30
 
@@ -61,28 +32,24 @@ describe Clavem::Authorizer do
61
32
  it "should handle default arguments" do
62
33
  authorizer = ::Clavem::Authorizer.new
63
34
  expect(authorizer.host).to eq("localhost")
64
- expect(authorizer.port).to eq(2501)
35
+ expect(authorizer.port).to eq(7772)
65
36
  expect(authorizer.command).to eq("open \"{{URL}}\"")
66
- expect(authorizer.title).to eq("Clavem Authorization")
67
- expect(authorizer.template).to eq(File.read(File.dirname(__FILE__) + "/../../lib/clavem/template.html.erb"))
68
37
  expect(authorizer.timeout).to eq(0)
69
- expect(authorizer.response_handler).to be_nil
38
+ expect(authorizer.instance_variable_get(:@response_handler)).to be_nil
70
39
  end
71
40
 
72
41
  it "should assign arguments" do
73
- authorizer = ::Clavem::Authorizer.new("HOST", 2511, "COMMAND", "TITLE", "TEMPLATE", 2) do end
42
+ authorizer = ::Clavem::Authorizer.new("HOST", 7773, "COMMAND", 2) do end
74
43
  expect(authorizer.host).to eq("HOST")
75
- expect(authorizer.port).to eq(2511)
44
+ expect(authorizer.port).to eq(7773)
76
45
  expect(authorizer.command).to eq("COMMAND")
77
- expect(authorizer.title).to eq("TITLE")
78
- expect(authorizer.template).to eq("TEMPLATE")
79
46
  expect(authorizer.timeout).to eq(2)
80
- expect(authorizer.response_handler).to be_a(Proc)
47
+ expect(authorizer.instance_variable_get(:@response_handler)).to be_a(Proc)
81
48
  end
82
49
 
83
50
  it "should correct wrong arguments" do
84
- authorizer = ::Clavem::Authorizer.new("IP", -10, nil, nil, "", -1)
85
- expect(authorizer.port).to eq(2501)
51
+ authorizer = ::Clavem::Authorizer.new("IP", -10, "", -1)
52
+ expect(authorizer.port).to eq(7772)
86
53
  expect(authorizer.timeout).to eq(0)
87
54
  end
88
55
 
@@ -98,86 +65,82 @@ describe Clavem::Authorizer do
98
65
  end
99
66
 
100
67
  describe "#authorize" do
101
- it "should call the correct authorize sequence and then return self" do
102
- sequence = []
103
- instance = ::Clavem::Authorizer.new
104
- server = ::ClavemDummyServer.new
105
-
106
- # Setup stuff
107
- allow(instance).to receive(:setup_webserver) do sequence << 1 end
68
+ it "should set authorization as waiting" do
69
+ allow(subject).to receive(:perform_request)
70
+ allow(subject).to receive(:process_response)
71
+ subject.authorize("URL")
72
+ expect(subject.waiting?).to be_true
73
+ end
108
74
 
109
- instance.instance_variable_set(:@server, server)
110
- allow(instance).to receive(:setup_interruptions_handling) do sequence << 2 end
111
- allow(instance).to receive(:setup_timeout_handling) do sequence << 3 end
112
- allow(instance).to receive(:open_endpoint) do sequence << 4 end
75
+ it "should make a request" do
76
+ allow(subject).to receive(:process_response)
77
+ allow(subject).to receive(:callback_url).and_return("CALLBACK")
113
78
 
114
- expect(server).to receive(:start)
115
- expect(instance.authorize("URL")).to be(instance)
116
- expect(sequence).to eq([1, 2, 3, 4])
79
+ expect(Kernel).to receive(:system).with("open \"URL?oauth_callback=CALLBACK\"")
80
+ subject.authorize("URL")
81
+ expect(Kernel).to receive(:system).with("open \"URL?&oauth_callback=CALLBACK\"")
82
+ subject.authorize("URL?")
83
+ subject.command = "browse {{URL}}"
84
+ expect(Kernel).to receive(:system).with("browse URL?oauth_callback=CALLBACK")
85
+ subject.authorize("URL")
117
86
  end
118
87
 
119
- it "should raise an exception in case of timeout" do
120
- instance = ::Clavem::Authorizer.new
121
- allow(instance).to receive(:setup_webserver).and_raise(::Clavem::Exceptions::Timeout)
122
- expect { instance.authorize("URL") }.to raise_error(::Clavem::Exceptions::Timeout)
123
- expect(instance.status).to eq(:failure)
88
+ it "should start a server" do
89
+ allow(subject).to receive(:perform_request)
90
+ subject.timeout = 1
91
+ expect(EM).to receive(:start_server).with(subject.host, subject.port, Clavem::Server, subject)
92
+ subject.authorize("URL")
124
93
  end
125
94
 
126
- it "should raise an exception in case of errors" do
127
- instance = ::Clavem::Authorizer.new
128
- allow(instance).to receive(:setup_webserver).and_raise(ArgumentError)
129
- expect { instance.authorize("URL") }.to raise_error(::Clavem::Exceptions::Failure)
130
- expect(instance.status).to eq(:failure)
131
- end
95
+ it "should return the success" do
96
+ allow(subject).to receive(:perform_request)
97
+
98
+ allow(subject).to receive(:process_response) { subject.status = :succeeded }
99
+ expect(subject.authorize("URL")).to be_true
132
100
 
133
- it "should always run #cleanup" do
134
- cleaned = false
135
- instance = ::Clavem::Authorizer.new
136
- allow(instance).to receive(:cleanup) do cleaned = true end
137
- allow(instance).to receive(:open_endpoint) do end
138
- allow(instance).to receive(:setup_webserver) do
139
- instance.instance_variable_set(:@server, ::ClavemDummyServer.new)
140
- end
101
+ allow(subject).to receive(:process_response) { subject.status = :failed }
102
+ expect(subject.authorize("URL")).to be_false
103
+ end
141
104
 
142
- cleaned = false
143
- instance.authorize("URL")
144
- expect(cleaned).to be_true
105
+ it "should handle errors" do
106
+ allow(Kernel).to receive(:system).and_raise(RuntimeError.new)
107
+ expect { subject.authorize("URL") }.to raise_error(Clavem::Exceptions::Failure)
108
+ expect(subject.failed?).to be_true
109
+ end
145
110
 
146
- cleaned = false
147
- allow(instance).to receive(:setup_webserver).and_raise(ArgumentError)
148
- expect { instance.authorize("URL") }.to raise_error(::Clavem::Exceptions::Failure)
149
- expect(cleaned).to be_true
111
+ it "should handle timeouts" do
112
+ allow(subject).to receive(:perform_request)
150
113
 
151
- cleaned = false
152
- allow(instance).to receive(:setup_webserver).and_raise(::Clavem::Exceptions::Timeout)
153
- expect { instance.authorize("URL") }.to raise_error(::Clavem::Exceptions::Timeout)
154
- expect(cleaned).to be_true
114
+ subject.timeout = 1
115
+ expect(EM).to receive(:stop).and_call_original
116
+ subject.authorize("URL")
155
117
  end
156
118
  end
157
119
 
158
120
  describe "#callback_url" do
159
121
  it "should return the correct callback" do
160
- expect(::Clavem::Authorizer.new.callback_url).to eq("http://localhost:2501/")
122
+ expect(::Clavem::Authorizer.new.callback_url).to eq("http://localhost:7772/")
161
123
  expect(::Clavem::Authorizer.new("10.0.0.1", "80").callback_url).to eq("http://10.0.0.1:80/")
162
124
  end
163
125
  end
164
126
 
165
- describe "#default_response_handler" do
166
- it "should return the token" do
167
- instance = ::Clavem::Authorizer.new
168
- expect(instance.default_response_handler(instance, ::ClavemDummyRequest.new("TOKEN"), nil)).to eq("TOKEN")
127
+ describe "#response_handler" do
128
+ it "should return the token as default implementation" do
129
+ expect(subject.response_handler.call(nil)).to be_nil
130
+ expect(subject.response_handler.call({"oauth_token" => "TOKEN"})).to eq("TOKEN")
131
+ expect(subject.response_handler.call({"oauth_token" => ["TOKEN 1", "TOKEN 2"]})).to eq("TOKEN 1")
169
132
  end
170
133
 
171
- it "should return an empty string by default" do
172
- instance = ::Clavem::Authorizer.new
173
- expect(instance.default_response_handler(instance, ::ClavemDummyRequest.new(nil), nil)).to eq("")
134
+ it "should work as a getter" do
135
+ subject.response_handler = "FOO"
136
+ expect(subject.response_handler).to eq("FOO")
174
137
  end
175
138
  end
176
139
 
177
140
  describe ".localize" do
178
141
  it "should set the right locale path" do
179
- expect(instance.instance_variable_get(:@i18n_locales_path)).to eq(File.absolute_path(::Pathname.new(File.dirname(__FILE__)).to_s + "/../../locales/"))
180
- instance.localize
142
+ expect(subject.instance_variable_get(:@i18n_locales_path)).to eq(File.absolute_path(::Pathname.new(File.dirname(__FILE__)).to_s + "/../../locales/"))
143
+ subject.localize
181
144
  end
182
145
 
183
146
  it "should set using English if called without arguments" do
@@ -193,160 +156,39 @@ describe Clavem::Authorizer do
193
156
  end
194
157
  end
195
158
 
196
- # PRIVATE
197
- describe "#open_endpoint" do
198
- it "should call system with the right command" do
199
- expect(Kernel).to receive(:system).with("open \"URL\"")
200
- instance.instance_variable_set(:@url, "URL")
201
- instance.send(:open_endpoint)
202
-
203
- expect(Kernel).to receive(:system).with("COMMAND")
204
- ::Clavem::Authorizer.new("HOST", "PORT", "COMMAND").send(:open_endpoint)
205
- end
206
-
207
- it "should raise exception in case of failures" do
208
- allow(Kernel).to receive(:system).and_raise(RuntimeError)
209
- expect { instance.send(:open_endpoint) }.to raise_error(::Clavem::Exceptions::Failure)
159
+ describe "#succeeded?" do
160
+ it "should return the correct status" do
161
+ subject.status = :other
162
+ expect(subject.succeeded?).to be_false
163
+ subject.status = :succeeded
164
+ expect(subject.succeeded?).to be_true
210
165
  end
211
166
  end
212
167
 
213
- describe "#setup_interruptions_handling" do
214
- it "should add handler for SIGINT, SIGTERM, SIGKILL" do
215
- expect(Kernel).to receive(:trap).with("USR2")
216
- expect(Kernel).to receive(:trap).with("INT")
217
- expect(Kernel).to receive(:trap).with("TERM")
218
- expect(Kernel).to receive(:trap).with("KILL")
219
- instance.send(:setup_interruptions_handling)
168
+ describe "#denied?" do
169
+ it "should return the correct status" do
170
+ subject.status = :other
171
+ expect(subject.denied?).to be_false
172
+ subject.status = :denied
173
+ expect(subject.denied?).to be_true
220
174
  end
221
175
  end
222
176
 
223
- describe "#setup_timeout_handling" do
224
- it "should not set a timeout handler by default" do
225
- authorizer = ::Clavem::Authorizer.new
226
- allow(authorizer).to receive(:open_endpoint)
227
- allow(authorizer).to receive(:setup_webserver) do authorizer.instance_variable_set(:@server, ::ClavemDummyServer.new) end
228
- authorizer.authorize("URL")
229
- expect(authorizer.instance_variable_get(:@timeout_handler)).to be_nil
230
- end
231
-
232
- it "should set and execute a timeout handler" do
233
- expect(Process).to receive(:kill).with("USR2", 0)
234
- expect(Kernel).to receive(:sleep).with(0.5)
235
-
236
- server = ::ClavemDummyServer.new
237
- allow(server).to receive(:start) do sleep(1) end
238
-
239
- authorizer = ::Clavem::Authorizer.new("HOST", "PORT", "COMMAND", "TITLE", "TEMPLATE", 500)
240
- allow(authorizer).to receive(:open_endpoint) do end
241
- allow(authorizer).to receive(:setup_webserver) do authorizer.instance_variable_set(:@server, server) end
242
- expect { authorizer.authorize("URL") }.to raise_error(::Clavem::Exceptions::Timeout)
243
-
244
- thread = authorizer.instance_variable_get(:@timeout_thread)
245
- expect(thread).to be_a(Thread)
246
- expect(authorizer.instance_variable_get(:@timeout_expired)).to be_true
177
+ describe "#failed?" do
178
+ it "should return the correct status" do
179
+ subject.status = :other
180
+ expect(subject.failed?).to be_false
181
+ subject.status = :failed
182
+ expect(subject.failed?).to be_true
247
183
  end
248
184
  end
249
185
 
250
- describe "#setup_webserver" do
251
- it "should initialize a web server with correct arguments" do
252
- logger = WEBrick::Log.new("/dev/null")
253
- allow(WEBrick::Log).to receive(:new).and_return(logger)
254
-
255
- expect(::WEBrick::HTTPServer).to receive(:new).with(BindAddress: "10.0.0.1", Port: 80, Logger: logger, AccessLog: [nil, nil]).and_return(::ClavemDummyServer.new)
256
- authorizer = ::Clavem::Authorizer.new("10.0.0.1", 80)
257
- authorizer.send(:setup_webserver)
258
- end
259
-
260
- it "should setup a single request handler on /" do
261
- server = ::ClavemDummyServer.new
262
- allow(::WEBrick::HTTPServer).to receive(:new).and_return(server)
263
- authorizer = ::Clavem::Authorizer.new("HOST", "PORT")
264
- expect(server).to receive(:mount_proc).with("/")
265
- authorizer.send(:setup_webserver)
266
- end
267
- end
268
-
269
- describe "#dispatch_request" do
270
- let(:request) { ::ClavemDummyRequest.new }
271
- let(:response) { ::ClavemDummyResponse.new }
272
- let(:server) { ::ClavemDummyServer.new }
273
-
274
- it "should call the correct handler" do
275
- instance.instance_variable_set(:@server, ::ClavemDummyServer.new)
276
- expect(instance).to receive(:default_response_handler).with(instance, request, response)
277
- instance.send(:dispatch_request, request, response)
278
-
279
- authorizer = ::Clavem::Authorizer.new do end
280
- authorizer.instance_variable_set(:@server, ::ClavemDummyServer.new)
281
- expect(authorizer.response_handler).to receive(:call).with(authorizer, request, response)
282
- authorizer.send(:dispatch_request, request, response)
283
- end
284
-
285
- it "should handle request only if the status is still :waiting" do
286
- authorizer = ::Clavem::Authorizer.new
287
- authorizer.instance_variable_set(:@server, server)
288
- authorizer.status = :waiting
289
- expect(server).to receive(:shutdown)
290
- authorizer.send(:dispatch_request, request, response)
291
-
292
- authorizer = ::Clavem::Authorizer.new
293
- authorizer.instance_variable_set(:@server, server)
294
- authorizer.status = :success
295
- expect(server).not_to receive(:shutdown)
296
- authorizer.send(:dispatch_request, request, response)
297
- end
298
-
299
- it "should correctly set status" do
300
- authorizer = ::Clavem::Authorizer.new
301
- authorizer.instance_variable_set(:@server, server)
302
- authorizer.send(:dispatch_request, ::ClavemDummyRequest.new("TOKEN"), response)
303
- expect(authorizer.status).to eq(:success)
304
- expect(response.status).to eq(200)
305
-
306
- authorizer = ::Clavem::Authorizer.new
307
- authorizer.instance_variable_set(:@server, server)
308
- authorizer.send(:dispatch_request, request, response)
309
- expect(authorizer.status).to eq(:denied)
310
- expect(response.status).to eq(403)
311
- end
312
-
313
- it "should render the body of the response" do
314
- authorizer = ::Clavem::Authorizer.new
315
- authorizer.instance_variable_set(:@server, server)
316
- expect(authorizer.instance_variable_get(:@compiled_template)).to receive(:result).and_return("TEMPLATE")
317
- authorizer.send(:dispatch_request, request, response)
318
- expect(response.body).to eq("TEMPLATE")
319
- end
320
- end
321
-
322
- describe "#cleanup" do
323
- it "should shutdown the server and cleanup signal handling" do
324
- server = ::ClavemDummyServer.new
325
- allow(server).to receive(:start) do sleep(1) end
326
-
327
- authorizer = ::Clavem::Authorizer.new("HOST", "PORT", "COMMAND", "TITLE", "TEMPLATE")
328
- allow(authorizer).to receive(:open_endpoint) do end
329
- allow(authorizer).to receive(:setup_webserver) do authorizer.instance_variable_set(:@server, server) end
330
- authorizer.authorize("URL")
331
-
332
- expect(Kernel).to receive(:trap).with("USR2", "DEFAULT")
333
- expect(Kernel).to receive(:trap).with("INT", "DEFAULT")
334
- expect(Kernel).to receive(:trap).with("TERM", "DEFAULT")
335
- expect(Kernel).to receive(:trap).with("KILL", "DEFAULT")
336
- expect(server).to receive(:shutdown)
337
- authorizer.send(:cleanup)
338
- end
339
-
340
- it "should exit timeout handling thread if active" do
341
- thread = nil
342
- server = ::ClavemDummyServer.new
343
- allow(server).to receive(:start) do sleep(1) end
344
-
345
- authorizer = ::Clavem::Authorizer.new("HOST", "PORT", "COMMAND", "TITLE", "TEMPLATE", 5000)
346
- authorizer.send(:setup_timeout_handling)
347
- thread = authorizer.instance_variable_get(:@timeout_thread)
348
- expect(thread).to receive(:exit)
349
- authorizer.send(:cleanup)
186
+ describe "#waiting?" do
187
+ it "should return the correct status" do
188
+ subject.status = :other
189
+ expect(subject.waiting?).to be_false
190
+ subject.status = :waiting
191
+ expect(subject.waiting?).to be_true
350
192
  end
351
193
  end
352
194
  end