datacatalog 0.1.0 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -4
- data/CHANGES.md +2 -2
- data/LICENSE.md +11 -11
- data/README.md +34 -31
- data/Rakefile +51 -49
- data/VERSION +1 -1
- data/datacatalog.gemspec +29 -13
- data/lib/base.rb +77 -0
- data/lib/datacatalog.rb +8 -28
- data/lib/main.rb +41 -0
- data/lib/resources/about.rb +11 -0
- data/lib/resources/api_key.rb +38 -0
- data/lib/resources/source.rb +31 -0
- data/lib/resources/user.rb +73 -0
- data/sandbox_api.yml.example +2 -2
- data/spec/about_spec.rb +21 -0
- data/spec/api_key_spec.rb +145 -1
- data/spec/base_spec.rb +129 -184
- data/spec/datacatalog_spec.rb +36 -0
- data/spec/source_spec.rb +162 -101
- data/spec/spec.opts +4 -4
- data/spec/spec_helper.rb +48 -48
- data/spec/user_spec.rb +278 -208
- metadata +54 -11
- data/doc/api_key_spec_for_rest_api.txt +0 -54
- data/doc/api_key_spec_for_ruby_api.rb +0 -26
- data/doc/mocking_options.md +0 -20
- data/lib/datacatalog/api_key.rb +0 -7
- data/lib/datacatalog/base.rb +0 -68
- data/lib/datacatalog/source.rb +0 -30
- data/lib/datacatalog/user.rb +0 -94
@@ -0,0 +1,36 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
include DataCatalog
|
3
|
+
|
4
|
+
describe DataCatalog do
|
5
|
+
|
6
|
+
describe "module accessors" do
|
7
|
+
it "should access the API Key" do
|
8
|
+
DataCatalog.api_key = "4159179f32ff8fefd2c6d48b7e675e7736bf1357"
|
9
|
+
DataCatalog.api_key.should == "4159179f32ff8fefd2c6d48b7e675e7736bf1357"
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should access the base URI" do
|
13
|
+
DataCatalog.base_uri = 'http://somehost.com'
|
14
|
+
DataCatalog.base_uri.should == 'http://somehost.com'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe ".with_key" do
|
19
|
+
it "should set the API key within the block" do
|
20
|
+
regular_key = '4159179f32ff8fefd2c6d48b7e675e7736bf1357'
|
21
|
+
DataCatalog.api_key = regular_key
|
22
|
+
temporary_key = '0000123400001234000012340000123400001234'
|
23
|
+
DataCatalog.with_key(temporary_key) do
|
24
|
+
DataCatalog.api_key.should == temporary_key
|
25
|
+
end
|
26
|
+
DataCatalog.api_key.should == regular_key
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should return the last value in the block" do
|
30
|
+
DataCatalog.with_key("0000444400004444") do
|
31
|
+
42
|
32
|
+
end.should == 42
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
data/spec/source_spec.rb
CHANGED
@@ -1,101 +1,162 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
def create_source(params={})
|
6
|
-
valid_params = {
|
7
|
-
:title => "Some FCC Data",
|
8
|
-
:url => "http://fcc.gov/somedata.csv"
|
9
|
-
}
|
10
|
-
|
11
|
-
end
|
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
|
-
end
|
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
|
-
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
include DataCatalog
|
3
|
+
|
4
|
+
module SourceHelpers
|
5
|
+
def create_source(params={})
|
6
|
+
valid_params = {
|
7
|
+
:title => "Some FCC Data",
|
8
|
+
:url => "http://fcc.gov/somedata.csv"
|
9
|
+
}
|
10
|
+
Source.create(valid_params.merge(params))
|
11
|
+
end
|
12
|
+
|
13
|
+
def create_3_sources
|
14
|
+
%w(FCC NASA DOE).each do |name|
|
15
|
+
Source.create({
|
16
|
+
:title => "#{name} Data",
|
17
|
+
:url => "http://#{name.downcase}.gov/data.xml"
|
18
|
+
})
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe Source do
|
24
|
+
include SourceHelpers
|
25
|
+
|
26
|
+
before do
|
27
|
+
setup_api
|
28
|
+
clean_slate
|
29
|
+
end
|
30
|
+
|
31
|
+
describe ".all" do
|
32
|
+
before do
|
33
|
+
create_3_sources
|
34
|
+
@sources = Source.all
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should return an enumeration of sources" do
|
38
|
+
@sources.each do |source|
|
39
|
+
source.should be_an_instance_of(Source)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should return correct titles" do
|
44
|
+
expected = ["FCC Data", "NASA Data", "DOE Data"]
|
45
|
+
@sources.map(&:title).sort.should == expected.sort
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe ".all with conditions" do
|
50
|
+
before do
|
51
|
+
create_3_sources
|
52
|
+
@sources = Source.all(:title => "NASA Data")
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should return an enumeration of sources" do
|
56
|
+
@sources.each do |source|
|
57
|
+
source.should be_an_instance_of(Source)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should return correct titles" do
|
62
|
+
expected = ["NASA Data"]
|
63
|
+
@sources.map(&:title).sort.should == expected.sort
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe ".create" do
|
68
|
+
it "should create a new source from basic params" do
|
69
|
+
source = create_source
|
70
|
+
source.should be_an_instance_of(Source)
|
71
|
+
source.url.should == "http://fcc.gov/somedata.csv"
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should create a new source from custom params" do
|
75
|
+
source = create_source({ :custom => {
|
76
|
+
"0" => {
|
77
|
+
:label => "License",
|
78
|
+
:description => "License",
|
79
|
+
:type => "string",
|
80
|
+
:value => "Public Domain"
|
81
|
+
}
|
82
|
+
}})
|
83
|
+
source.should be_an_instance_of(Source)
|
84
|
+
source.url.should == "http://fcc.gov/somedata.csv"
|
85
|
+
source.custom.should == {
|
86
|
+
"0" => {
|
87
|
+
"label" => "License",
|
88
|
+
"description" => "License",
|
89
|
+
"type" => "string",
|
90
|
+
"value" => "Public Domain"
|
91
|
+
}
|
92
|
+
}
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe ".first" do
|
97
|
+
before do
|
98
|
+
create_3_sources
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should return a source" do
|
102
|
+
source = Source.first(:title => "NASA Data")
|
103
|
+
source.should be_an_instance_of(Source)
|
104
|
+
source.title.should == "NASA Data"
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should return nil if nothing found" do
|
108
|
+
source = Source.first(:title => "UFO Data")
|
109
|
+
source.should be_nil
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
describe ".get" do
|
114
|
+
before do
|
115
|
+
@source = create_source
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should return a source" do
|
119
|
+
source = Source.get(@source.id)
|
120
|
+
source.should be_an_instance_of(Source)
|
121
|
+
source.title.should == "Some FCC Data"
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should raise NotFound if no source exists" do
|
125
|
+
executing do
|
126
|
+
Source.get(mangle(@source.id))
|
127
|
+
end.should raise_error(NotFound)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
describe ".update" do
|
132
|
+
before do
|
133
|
+
@source = create_source
|
134
|
+
end
|
135
|
+
|
136
|
+
it "should update an existing source from valid params" do
|
137
|
+
source = Source.update(@source.id, {
|
138
|
+
:url => "http://fec.gov/newdata.csv"
|
139
|
+
})
|
140
|
+
source.should be_an_instance_of(Source)
|
141
|
+
source.url.should == "http://fec.gov/newdata.csv"
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
describe ".destroy" do
|
146
|
+
before do
|
147
|
+
@source = create_source
|
148
|
+
end
|
149
|
+
|
150
|
+
it "should destroy an existing source" do
|
151
|
+
result = Source.destroy(@source.id)
|
152
|
+
result.should be_true
|
153
|
+
end
|
154
|
+
|
155
|
+
it "should raise NotFound when attempting to destroy non-existing source" do
|
156
|
+
executing do
|
157
|
+
Source.destroy(mangle(@source.id))
|
158
|
+
end.should raise_error(NotFound)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
end
|
data/spec/spec.opts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
--color
|
2
|
-
--timeout
|
3
|
-
20
|
4
|
-
--diff
|
1
|
+
--color
|
2
|
+
--timeout
|
3
|
+
20
|
4
|
+
--diff
|
data/spec/spec_helper.rb
CHANGED
@@ -1,48 +1,48 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../lib/datacatalog'
|
2
|
-
require 'yaml'
|
3
|
-
|
4
|
-
Spec::Runner.configure do |config|
|
5
|
-
config.mock_with :rr
|
6
|
-
end
|
7
|
-
|
8
|
-
alias :executing :lambda
|
9
|
-
|
10
|
-
def setup_api
|
11
|
-
config = YAML.load_file(File.dirname(__FILE__) + '/../sandbox_api.yml')
|
12
|
-
DataCatalog.api_key = config['api_key']
|
13
|
-
DataCatalog.base_uri = config['base_uri']
|
14
|
-
end
|
15
|
-
|
16
|
-
def clean_slate
|
17
|
-
DataCatalog::User.all.each do |u|
|
18
|
-
DataCatalog::User.destroy(u.id) unless u.name == "Primary Admin"
|
19
|
-
end
|
20
|
-
DataCatalog::Source.all.each do |s|
|
21
|
-
DataCatalog::Source.destroy(s.id)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
if RUBY_VERSION >= "1.8.7"
|
26
|
-
# Converts a valid ID into a almost-but-not-quite valid one.
|
27
|
-
# Here is an example of what it does:
|
28
|
-
# From ... 4ac2520b25b7e7056600034e
|
29
|
-
# To ... a42c25b0527b7e50660030e4
|
30
|
-
def mangle(string)
|
31
|
-
array = string.chars.to_a
|
32
|
-
sliced = []
|
33
|
-
array.each_slice(2) { |s| sliced << s.reverse }
|
34
|
-
result = sliced.flatten.join
|
35
|
-
raise "mangle failed" if result == string
|
36
|
-
result
|
37
|
-
end
|
38
|
-
else
|
39
|
-
# Converts a valid ID into a almost-but-not-quite valid one.
|
40
|
-
# Here is an example of what it does:
|
41
|
-
# From ... 4ac2520b25b7e7056600034e
|
42
|
-
# To ... e4300066507e7b52b0252ca4
|
43
|
-
def mangle(string)
|
44
|
-
result = string.reverse
|
45
|
-
raise "mangle failed" if result == string
|
46
|
-
result
|
47
|
-
end
|
48
|
-
end
|
1
|
+
require File.dirname(__FILE__) + '/../lib/datacatalog'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
Spec::Runner.configure do |config|
|
5
|
+
config.mock_with :rr
|
6
|
+
end
|
7
|
+
|
8
|
+
alias :executing :lambda
|
9
|
+
|
10
|
+
def setup_api
|
11
|
+
config = YAML.load_file(File.dirname(__FILE__) + '/../sandbox_api.yml')
|
12
|
+
DataCatalog.api_key = config['api_key']
|
13
|
+
DataCatalog.base_uri = config['base_uri']
|
14
|
+
end
|
15
|
+
|
16
|
+
def clean_slate
|
17
|
+
DataCatalog::User.all.each do |u|
|
18
|
+
DataCatalog::User.destroy(u.id) unless u.name == "Primary Admin"
|
19
|
+
end
|
20
|
+
DataCatalog::Source.all.each do |s|
|
21
|
+
DataCatalog::Source.destroy(s.id)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
if RUBY_VERSION >= "1.8.7"
|
26
|
+
# Converts a valid ID into a almost-but-not-quite valid one.
|
27
|
+
# Here is an example of what it does:
|
28
|
+
# From ... 4ac2520b25b7e7056600034e
|
29
|
+
# To ... a42c25b0527b7e50660030e4
|
30
|
+
def mangle(string)
|
31
|
+
array = string.chars.to_a
|
32
|
+
sliced = []
|
33
|
+
array.each_slice(2) { |s| sliced << s.reverse }
|
34
|
+
result = sliced.flatten.join
|
35
|
+
raise "mangle failed" if result == string
|
36
|
+
result
|
37
|
+
end
|
38
|
+
else
|
39
|
+
# Converts a valid ID into a almost-but-not-quite valid one.
|
40
|
+
# Here is an example of what it does:
|
41
|
+
# From ... 4ac2520b25b7e7056600034e
|
42
|
+
# To ... e4300066507e7b52b0252ca4
|
43
|
+
def mangle(string)
|
44
|
+
result = string.reverse
|
45
|
+
raise "mangle failed" if result == string
|
46
|
+
result
|
47
|
+
end
|
48
|
+
end
|
data/spec/user_spec.rb
CHANGED
@@ -1,208 +1,278 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
def create_user
|
6
|
-
|
7
|
-
:name => "Ted Smith",
|
8
|
-
:email => "ted@email.com"
|
9
|
-
})
|
10
|
-
end
|
11
|
-
|
12
|
-
def create_user_with_2_keys
|
13
|
-
user = create_user
|
14
|
-
result = user.generate_api_key!(
|
15
|
-
:purpose => "Civic hacking with my awesome app",
|
16
|
-
:key_type => "application"
|
17
|
-
)
|
18
|
-
raise "generate_api_key! failed" unless result
|
19
|
-
raise "incorrect number of keys" unless user.api_keys.length == 2
|
20
|
-
user
|
21
|
-
end
|
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
|
-
@user
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
include DataCatalog
|
3
|
+
|
4
|
+
module UserHelpers
|
5
|
+
def create_user
|
6
|
+
User.create({
|
7
|
+
:name => "Ted Smith",
|
8
|
+
:email => "ted@email.com"
|
9
|
+
})
|
10
|
+
end
|
11
|
+
|
12
|
+
def create_user_with_2_keys
|
13
|
+
user = create_user
|
14
|
+
result = user.generate_api_key!(
|
15
|
+
:purpose => "Civic hacking with my awesome app",
|
16
|
+
:key_type => "application"
|
17
|
+
)
|
18
|
+
raise "generate_api_key! failed" unless result
|
19
|
+
raise "incorrect number of keys" unless user.api_keys.length == 2
|
20
|
+
user
|
21
|
+
end
|
22
|
+
|
23
|
+
def create_3_users
|
24
|
+
3.times do |n|
|
25
|
+
User.create(
|
26
|
+
:name => "User-#{n}",
|
27
|
+
:email => "user_#{n}@email.com"
|
28
|
+
)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe User do
|
34
|
+
include UserHelpers
|
35
|
+
|
36
|
+
before do
|
37
|
+
setup_api
|
38
|
+
clean_slate
|
39
|
+
end
|
40
|
+
|
41
|
+
describe ".all" do
|
42
|
+
before do
|
43
|
+
create_3_users
|
44
|
+
@users = User.all
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should return an enumeration of users" do
|
48
|
+
@users.each do |u|
|
49
|
+
u.should be_an_instance_of(User)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should include four users" do
|
54
|
+
names = @users.map(&:name)
|
55
|
+
names.should include("User-0")
|
56
|
+
names.should include("User-1")
|
57
|
+
names.should include("User-2")
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe ".create" do
|
62
|
+
before do
|
63
|
+
@user = create_user
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should create a new user when valid params are passed in" do
|
67
|
+
@user.should be_an_instance_of(User)
|
68
|
+
@user.name.should == "Ted Smith"
|
69
|
+
@user.email.should == "ted@email.com"
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should raise BadRequest when invalid params are passed in" do
|
73
|
+
executing do
|
74
|
+
User.create({ :garbage_field => "junk" })
|
75
|
+
end.should raise_error(BadRequest)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe ".first" do
|
80
|
+
before do
|
81
|
+
create_3_users
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should return a user" do
|
85
|
+
user = User.first(:name => "User-1")
|
86
|
+
user.should be_an_instance_of(User)
|
87
|
+
user.name.should == "User-1"
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should return nil if nothing found" do
|
91
|
+
user = User.first(:name => "Elvis")
|
92
|
+
user.should be_nil
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe ".get_by_api_key" do
|
97
|
+
before do
|
98
|
+
@user = create_user
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should return a user" do
|
102
|
+
user = User.get_by_api_key(@user.primary_api_key)
|
103
|
+
user.should be_an_instance_of(User)
|
104
|
+
user.email.should == "ted@email.com"
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
describe ".get" do
|
109
|
+
before do
|
110
|
+
@user = create_user_with_2_keys
|
111
|
+
end
|
112
|
+
|
113
|
+
describe "user exists" do
|
114
|
+
before do
|
115
|
+
@u = User.get(@user.id)
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should return a user" do
|
119
|
+
@u.should be_an_instance_of(User)
|
120
|
+
@u.name.should == "Ted Smith"
|
121
|
+
@u.email.should == "ted@email.com"
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should include 2 api_keys" do
|
125
|
+
keys = @u.api_keys
|
126
|
+
keys.map(&:key_type).should == %w(primary application)
|
127
|
+
keys.each do |key|
|
128
|
+
key.should be_an_instance_of(ApiKey)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
it "should raise NotFound if no user exists" do
|
134
|
+
executing do
|
135
|
+
User.get(mangle(@user.id))
|
136
|
+
end.should raise_error(NotFound)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
describe ".get_by_api_key" do
|
141
|
+
before do
|
142
|
+
@user = create_user_with_2_keys
|
143
|
+
end
|
144
|
+
|
145
|
+
describe "API key exists" do
|
146
|
+
before do
|
147
|
+
@u = User.get_by_api_key(@user.primary_api_key)
|
148
|
+
end
|
149
|
+
|
150
|
+
it "should return a user" do
|
151
|
+
@u.should be_an_instance_of(User)
|
152
|
+
@u.name.should == "Ted Smith"
|
153
|
+
@u.email.should == "ted@email.com"
|
154
|
+
end
|
155
|
+
|
156
|
+
it "should include 2 api_keys" do
|
157
|
+
keys = @u.api_keys
|
158
|
+
keys.map(&:key_type).should == %w(primary application)
|
159
|
+
keys.each do |key|
|
160
|
+
key.should be_an_instance_of(ApiKey)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
it "should raise NotFound if API key does not exist" do
|
166
|
+
executing do
|
167
|
+
User.get_by_api_key(mangle(@user.primary_api_key))
|
168
|
+
end.should raise_error(NotFound)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
describe ".update" do
|
173
|
+
before do
|
174
|
+
@user = create_user
|
175
|
+
end
|
176
|
+
|
177
|
+
it "should update a user when valid params are passed in" do
|
178
|
+
user = User.update(@user.id, { :name => "Jane Smith" })
|
179
|
+
user.name.should == "Jane Smith"
|
180
|
+
end
|
181
|
+
|
182
|
+
it "should raise BadRequest when invalid params are passed in" do
|
183
|
+
executing do
|
184
|
+
User.update(@user.id, { :garbage => "junk" })
|
185
|
+
end.should raise_error(BadRequest)
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
describe ".destroy" do
|
190
|
+
before do
|
191
|
+
@user = create_user
|
192
|
+
end
|
193
|
+
|
194
|
+
it "should destroy an existing user" do
|
195
|
+
result = User.destroy(@user.id)
|
196
|
+
result.should be_true
|
197
|
+
end
|
198
|
+
|
199
|
+
it "should raise NotFound when non-existing user" do
|
200
|
+
executing do
|
201
|
+
User.destroy(mangle(@user.id))
|
202
|
+
end.should raise_error(NotFound)
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
describe "#generate_api_key!" do
|
207
|
+
before do
|
208
|
+
@user = create_user
|
209
|
+
end
|
210
|
+
|
211
|
+
it "should generate a new key for the user" do
|
212
|
+
@user.api_keys.length.should == 1
|
213
|
+
@user.generate_api_key!({
|
214
|
+
:purpose => "Civic hacking with my awesome app",
|
215
|
+
:key_type => "application"
|
216
|
+
}).should be_true
|
217
|
+
@user.api_keys.length.should == 2
|
218
|
+
@user.api_keys.last[:purpose].should == "Civic hacking with my awesome app"
|
219
|
+
@user.application_api_keys.length.should == 1
|
220
|
+
end
|
221
|
+
|
222
|
+
it "should raise BadRequest when attempting to create a primary key" do
|
223
|
+
executing do
|
224
|
+
@user.generate_api_key!({
|
225
|
+
:purpose => "Civic hacking with my awesome app",
|
226
|
+
:key_type => "primary"
|
227
|
+
})
|
228
|
+
end.should raise_error(BadRequest)
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
describe "#update_api_key!" do
|
233
|
+
before do
|
234
|
+
@user = create_user_with_2_keys
|
235
|
+
end
|
236
|
+
|
237
|
+
it "should update a key for the user" do
|
238
|
+
@user.update_api_key!(@user.api_keys[1].id, {
|
239
|
+
:key_type => "valet",
|
240
|
+
:purpose => "To be more awesome"
|
241
|
+
}).should be_true
|
242
|
+
@user.api_keys[1].purpose.should == "To be more awesome"
|
243
|
+
end
|
244
|
+
|
245
|
+
it "should raise NotFound if updating a key that doesn't exist" do
|
246
|
+
executing do
|
247
|
+
@user.update_api_key!(mangle(@user.api_keys[1].id), {})
|
248
|
+
end.should raise_error(NotFound)
|
249
|
+
end
|
250
|
+
|
251
|
+
it "should raise BadRequest if primary key's type is changed" do
|
252
|
+
executing do
|
253
|
+
@user.update_api_key!(@user.api_keys[0].id, {
|
254
|
+
:key_type => "valet"
|
255
|
+
})
|
256
|
+
end.should raise_error(BadRequest)
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
describe "#delete_api_key!" do
|
261
|
+
before do
|
262
|
+
@user = create_user_with_2_keys
|
263
|
+
end
|
264
|
+
|
265
|
+
it "should delete a key for the user" do
|
266
|
+
@user.delete_api_key!(@user.api_keys[1].id).should be_true
|
267
|
+
@user.api_keys.length.should == 1
|
268
|
+
end
|
269
|
+
|
270
|
+
it "should raise Conflict if deleting the primary key" do
|
271
|
+
executing do
|
272
|
+
@user.delete_api_key!(@user.api_keys[0].id)
|
273
|
+
end.should raise_error(Conflict)
|
274
|
+
@user.api_keys.length.should == 2
|
275
|
+
end
|
276
|
+
end
|
277
|
+
|
278
|
+
end
|