datacatalog 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/spec/spec.opts CHANGED
@@ -1,4 +1,3 @@
1
- --color
2
- --timeout
3
- 20
4
- --diff
1
+ --color
2
+ --timeout
3
+ 20
data/spec/spec_helper.rb CHANGED
@@ -8,35 +8,25 @@ end
8
8
  alias :executing :lambda
9
9
 
10
10
  def clean_slate
11
+
11
12
  DataCatalog::User.all.each do |u|
12
13
  DataCatalog::User.destroy(u.id) unless u.name == "Primary Admin"
13
14
  end
14
- DataCatalog::Source.all.each do |s|
15
- DataCatalog::Source.destroy(s.id)
15
+
16
+ classes = [DataCatalog::Source, DataCatalog::Organization]
17
+ classes.each do |class_constant|
18
+ class_constant.all.each do |instance|
19
+ class_constant.destroy(instance.id)
20
+ end
16
21
  end
22
+
17
23
  end
18
24
 
19
- if RUBY_VERSION >= "1.8.7"
20
- # Converts a valid ID into a almost-but-not-quite valid one.
21
- # Here is an example of what it does:
22
- # From ... 4ac2520b25b7e7056600034e
23
- # To ... a42c25b0527b7e50660030e4
24
- def mangle(string)
25
- array = string.chars.to_a
26
- sliced = []
27
- array.each_slice(2) { |s| sliced << s.reverse }
28
- result = sliced.flatten.join
29
- raise "mangle failed" if result == string
30
- result
31
- end
32
- else
33
- # Converts a valid ID into a almost-but-not-quite valid one.
34
- # Here is an example of what it does:
35
- # From ... 4ac2520b25b7e7056600034e
36
- # To ... e4300066507e7b52b0252ca4
37
- def mangle(string)
38
- result = string.reverse
39
- raise "mangle failed" if result == string
40
- result
41
- end
25
+ def mangle(string)
26
+ array = string.chars.to_a
27
+ sliced = []
28
+ array.each_slice(2) { |s| sliced << s.reverse }
29
+ result = sliced.flatten.join
30
+ raise "mangle failed" if result == string
31
+ result
42
32
  end
data/spec/user_spec.rb CHANGED
@@ -1,278 +1,279 @@
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
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
+ @user.created_at.should be_an_instance_of(Time)
71
+ end
72
+
73
+ it "should raise BadRequest when invalid params are passed in" do
74
+ executing do
75
+ User.create({ :garbage_field => "junk" })
76
+ end.should raise_error(BadRequest)
77
+ end
78
+ end
79
+
80
+ describe ".first" do
81
+ before do
82
+ create_3_users
83
+ end
84
+
85
+ it "should return a user" do
86
+ user = User.first(:name => "User-1")
87
+ user.should be_an_instance_of(User)
88
+ user.name.should == "User-1"
89
+ end
90
+
91
+ it "should return nil if nothing found" do
92
+ user = User.first(:name => "Elvis")
93
+ user.should be_nil
94
+ end
95
+ end
96
+
97
+ describe ".get_by_api_key" do
98
+ before do
99
+ @user = create_user
100
+ end
101
+
102
+ it "should return a user" do
103
+ user = User.get_by_api_key(@user.primary_api_key)
104
+ user.should be_an_instance_of(User)
105
+ user.email.should == "ted@email.com"
106
+ end
107
+ end
108
+
109
+ describe ".get" do
110
+ before do
111
+ @user = create_user_with_2_keys
112
+ end
113
+
114
+ describe "user exists" do
115
+ before do
116
+ @u = User.get(@user.id)
117
+ end
118
+
119
+ it "should return a user" do
120
+ @u.should be_an_instance_of(User)
121
+ @u.name.should == "Ted Smith"
122
+ @u.email.should == "ted@email.com"
123
+ end
124
+
125
+ it "should include 2 api_keys" do
126
+ keys = @u.api_keys
127
+ keys.map(&:key_type).should == %w(primary application)
128
+ keys.each do |key|
129
+ key.should be_an_instance_of(ApiKey)
130
+ end
131
+ end
132
+ end
133
+
134
+ it "should raise NotFound if no user exists" do
135
+ executing do
136
+ User.get(mangle(@user.id))
137
+ end.should raise_error(NotFound)
138
+ end
139
+ end
140
+
141
+ describe ".get_by_api_key" do
142
+ before do
143
+ @user = create_user_with_2_keys
144
+ end
145
+
146
+ describe "API key exists" do
147
+ before do
148
+ @u = User.get_by_api_key(@user.primary_api_key)
149
+ end
150
+
151
+ it "should return a user" do
152
+ @u.should be_an_instance_of(User)
153
+ @u.name.should == "Ted Smith"
154
+ @u.email.should == "ted@email.com"
155
+ end
156
+
157
+ it "should include 2 api_keys" do
158
+ keys = @u.api_keys
159
+ keys.map(&:key_type).should == %w(primary application)
160
+ keys.each do |key|
161
+ key.should be_an_instance_of(ApiKey)
162
+ end
163
+ end
164
+ end
165
+
166
+ it "should raise NotFound if API key does not exist" do
167
+ executing do
168
+ User.get_by_api_key(mangle(@user.primary_api_key))
169
+ end.should raise_error(NotFound)
170
+ end
171
+ end
172
+
173
+ describe ".update" do
174
+ before do
175
+ @user = create_user
176
+ end
177
+
178
+ it "should update a user when valid params are passed in" do
179
+ user = User.update(@user.id, { :name => "Jane Smith" })
180
+ user.name.should == "Jane Smith"
181
+ end
182
+
183
+ it "should raise BadRequest when invalid params are passed in" do
184
+ executing do
185
+ User.update(@user.id, { :garbage => "junk" })
186
+ end.should raise_error(BadRequest)
187
+ end
188
+ end
189
+
190
+ describe ".destroy" do
191
+ before do
192
+ @user = create_user
193
+ end
194
+
195
+ it "should destroy an existing user" do
196
+ result = User.destroy(@user.id)
197
+ result.should be_true
198
+ end
199
+
200
+ it "should raise NotFound when non-existing user" do
201
+ executing do
202
+ User.destroy(mangle(@user.id))
203
+ end.should raise_error(NotFound)
204
+ end
205
+ end
206
+
207
+ describe "#generate_api_key!" do
208
+ before do
209
+ @user = create_user
210
+ end
211
+
212
+ it "should generate a new key for the user" do
213
+ @user.api_keys.length.should == 1
214
+ @user.generate_api_key!({
215
+ :purpose => "Civic hacking with my awesome app",
216
+ :key_type => "application"
217
+ }).should be_true
218
+ @user.api_keys.length.should == 2
219
+ @user.api_keys.last[:purpose].should == "Civic hacking with my awesome app"
220
+ @user.application_api_keys.length.should == 1
221
+ end
222
+
223
+ it "should raise BadRequest when attempting to create a primary key" do
224
+ executing do
225
+ @user.generate_api_key!({
226
+ :purpose => "Civic hacking with my awesome app",
227
+ :key_type => "primary"
228
+ })
229
+ end.should raise_error(BadRequest)
230
+ end
231
+ end
232
+
233
+ describe "#update_api_key!" do
234
+ before do
235
+ @user = create_user_with_2_keys
236
+ end
237
+
238
+ it "should update a key for the user" do
239
+ @user.update_api_key!(@user.api_keys[1].id, {
240
+ :key_type => "valet",
241
+ :purpose => "To be more awesome"
242
+ }).should be_true
243
+ @user.api_keys[1].purpose.should == "To be more awesome"
244
+ end
245
+
246
+ it "should raise NotFound if updating a key that doesn't exist" do
247
+ executing do
248
+ @user.update_api_key!(mangle(@user.api_keys[1].id), {})
249
+ end.should raise_error(NotFound)
250
+ end
251
+
252
+ it "should raise BadRequest if primary key's type is changed" do
253
+ executing do
254
+ @user.update_api_key!(@user.api_keys[0].id, {
255
+ :key_type => "valet"
256
+ })
257
+ end.should raise_error(BadRequest)
258
+ end
259
+ end
260
+
261
+ describe "#delete_api_key!" do
262
+ before do
263
+ @user = create_user_with_2_keys
264
+ end
265
+
266
+ it "should delete a key for the user" do
267
+ @user.delete_api_key!(@user.api_keys[1].id).should be_true
268
+ @user.api_keys.length.should == 1
269
+ end
270
+
271
+ it "should raise Conflict if deleting the primary key" do
272
+ executing do
273
+ @user.delete_api_key!(@user.api_keys[0].id)
274
+ end.should raise_error(Conflict)
275
+ @user.api_keys.length.should == 2
276
+ end
277
+ end
278
+
279
+ end