This repository has been archived by the owner on Jan 31, 2020. It is now read-only.
/
test_match_key.rb
165 lines (128 loc) · 2.84 KB
/
test_match_key.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
require 'test/helper'
require 'test/unit'
require 'rack/test'
require 'sinatra'
require 'sinatra/rest-service-auth'
require 'digest'
ENV['RACK_ENV'] = 'test'
class App1 < Sinatra::Base
register Sinatra::RESTServiceAuth
set :keys, %w{34 54}
get '/' do
'ok'
end
end
class TestKeyFromEnumerable < Test::Unit::TestCase
include Rack::Test::Methods
def app
App1.new
end
def test_auth
q = '?key=34'
url = "http://example.org/#{q}"
sig = Digest::SHA2.new(256).hexdigest(url)
get "/#{q}&sig=#{sig}"
assert last_response.ok?, "request should succeed"
end
end
class TestModel
attr_reader :key
def initialize(key)
@key = key
end
def self.all
%w{34 54}.map { |k| self.new(k) }
end
end
class App2 < Sinatra::Base
register Sinatra::RESTServiceAuth
set :keys, TestModel
get '/' do
'ok'
end
end
class TestKeyFromModel < Test::Unit::TestCase
include Rack::Test::Methods
def app
App2.new
end
def test_all_method
assert TestModel.respond_to?(:all)
end
def test_select
assert !TestModel.all.select { |x| x.key == '34' }.empty?
end
def test_auth
q = '?key=34'
url = "http://example.org/#{q}"
sig = Digest::SHA2.new(256).hexdigest(url)
get "/#{q}&sig=#{sig}"
assert last_response.ok?, "request should succeed"
end
end
class App3 < Sinatra::Base
register Sinatra::RESTServiceAuth
set :keys, lambda { %w{34 54} }
get '/' do
'ok'
end
end
class TestKeyFromLambda < Test::Unit::TestCase
include Rack::Test::Methods
def app
App3.new
end
def test_auth
q = '?key=34'
url = "http://example.org/#{q}"
sig = Digest::SHA2.new(256).hexdigest(url)
get "/#{q}&sig=#{sig}"
assert last_response.ok?, "request should succeed"
end
end
class App4 < Sinatra::Base
register Sinatra::RESTServiceAuth
authorize_when { |k, s| k == '34' }
get '/' do
'ok'
end
end
class TestMatchKey < Test::Unit::TestCase
include Rack::Test::Methods
def app
App4.new
end
def test_auth
q = '?key=34'
url = "http://example.org/#{q}"
sig = Digest::SHA2.new(256).hexdigest(url)
get "/#{q}&sig=#{sig}"
File.open('errors.html', 'w') do |f|
f.write last_response.body
end
assert last_response.ok?, "request should succeed"
end
end
class App5 < Sinatra::Base
register Sinatra::RESTServiceAuth
set :keys, 'config-single.yml'
get '/' do
'ok'
end
end
class TestKeyFromFileWithSingleKey < Test::Unit::TestCase
include Rack::Test::Methods
def app
App5.new
end
def test_file_exists
assert File.exists?(File.expand_path(File.join(settings.root, 'config-single.yml')))
end
def test_auth
q = '?key=34'
url = "http://example.org/#{q}"
sig = Digest::SHA2.new(256).hexdigest(url)
get "/#{q}&sig=#{sig}"
assert last_response.ok?, "request should succeed"
end
end