datacatalog 0.1.0 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
- describe DataCatalog::Source do
4
-
5
- def create_source(params={})
6
- valid_params = {
7
- :title => "Some FCC Data",
8
- :url => "http://fcc.gov/somedata.csv"
9
- }
10
- DataCatalog::Source.create(valid_params.merge(params))
11
- end
12
-
13
- before(:each) do
14
- setup_api
15
- clean_slate
16
- end
17
-
18
- describe ".all" do
19
- before do
20
- %w(FCC NASA DOE).each do |name|
21
- DataCatalog::Source.create({
22
- :title => "#{name} Data",
23
- :url => "http://#{name.downcase}.gov/data.xml"
24
- })
25
- end
26
- @sources = DataCatalog::Source.all
27
- end
28
-
29
- it "should return an enumeration of sources" do
30
- @sources.each do |source|
31
- source.should be_an_instance_of(DataCatalog::Source)
32
- end
33
- end
34
-
35
- it "should return correct titles" do
36
- expected = ["FCC Data", "NASA Data", "DOE Data"]
37
- @sources.map(&:title).sort.should == expected.sort
38
- end
39
- end # describe ".all"
40
-
41
- describe ".create" do
42
- it "should create a new source from basic params" do
43
- source = create_source
44
- source.should be_an_instance_of(DataCatalog::Source)
45
- source.url.should == "http://fcc.gov/somedata.csv"
46
- end
47
-
48
- it "should create a new source from custom params" do
49
- source = create_source({ :custom => {
50
- "0" => {
51
- :label => "License",
52
- :description => "License",
53
- :type => "string",
54
- :value => "Public Domain"
55
- }
56
- }})
57
- source.should be_an_instance_of(DataCatalog::Source)
58
- source.url.should == "http://fcc.gov/somedata.csv"
59
- source.custom.should == {
60
- "0" => {
61
- "label" => "License",
62
- "description" => "License",
63
- "type" => "string",
64
- "value" => "Public Domain"
65
- }
66
- }
67
- end
68
- end # describe ".all"
69
-
70
- describe ".update" do
71
- before do
72
- @source = create_source
73
- end
74
-
75
- it "should update an existing source from valid params" do
76
- source = DataCatalog::Source.update(@source.id, {
77
- :url => "http://fec.gov/newdata.csv"
78
- })
79
- source.should be_an_instance_of(DataCatalog::Source)
80
- source.url.should == "http://fec.gov/newdata.csv"
81
- end
82
- end # describe ".all"
83
-
84
- describe ".destroy" do
85
- before do
86
- @source = create_source
87
- end
88
-
89
- it "should destroy an existing source" do
90
- result = DataCatalog::Source.destroy(@source.id)
91
- result.should be_true
92
- end
93
-
94
- it "should raise NotFound when attempting to destroy non-existing source" do
95
- executing do
96
- DataCatalog::Source.destroy(mangle(@source.id))
97
- end.should raise_error(DataCatalog::NotFound)
98
- end
99
- end # describe ".destroy"
100
-
101
- end
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
- describe DataCatalog::User do
4
-
5
- def create_user
6
- DataCatalog::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
- before(:each) do
24
- setup_api
25
- clean_slate
26
- end
27
-
28
- describe ".all" do
29
- before(:each) do
30
- 3.times do |n|
31
- DataCatalog::User.create(
32
- :name => "User-#{n}",
33
- :email => "user_#{n}@email.com"
34
- )
35
- end
36
- @users = DataCatalog::User.all
37
- end
38
-
39
- it "should return an enumeration of users" do
40
- @users.each do |u|
41
- u.should be_an_instance_of(DataCatalog::User)
42
- end
43
- end
44
-
45
- it "should include four users" do
46
- names = @users.map(&:name)
47
- names.should include("User-0")
48
- names.should include("User-1")
49
- names.should include("User-2")
50
- end
51
- end # describe ".all"
52
-
53
- describe ".create" do
54
- before do
55
- @user = create_user
56
- end
57
-
58
- it "should create a new user when valid params are passed in" do
59
- @user.should be_an_instance_of(DataCatalog::User)
60
- @user.name.should == "Ted Smith"
61
- @user.email.should == "ted@email.com"
62
- end
63
-
64
- it "should raise BadRequest when invalid params are passed in" do
65
- executing do
66
- DataCatalog::User.create({ :garbage_field => "junk" })
67
- end.should raise_error(DataCatalog::BadRequest)
68
- end
69
- end # describe ".create"
70
-
71
- describe ".find" do
72
- before do
73
- @user = create_user
74
- end
75
-
76
- it "should return a user" do
77
- user = DataCatalog::User.find(@user.id)
78
- user.should be_an_instance_of(DataCatalog::User)
79
- user.email.should == "ted@email.com"
80
- end
81
-
82
- it "should raise NotFound out if no user exists" do
83
- executing do
84
- DataCatalog::User.find(mangle(@user.id))
85
- end.should raise_error(DataCatalog::NotFound)
86
- end
87
- end # describe ".find"
88
-
89
- describe ".find_by_api_key" do
90
- before do
91
- @user = create_user
92
- end
93
-
94
- it "should return a user" do
95
- user = DataCatalog::User.find_by_api_key(@user.primary_api_key)
96
- user.should be_an_instance_of(DataCatalog::User)
97
- user.email.should == "ted@email.com"
98
- end
99
- end # describe ".find_by_api_key"
100
-
101
- describe ".update" do
102
- before do
103
- @user = create_user
104
- end
105
-
106
- it "should update a user when valid params are passed in" do
107
- user = DataCatalog::User.update(@user.id, { :name => "Jane Smith" })
108
- user.name.should == "Jane Smith"
109
- end
110
-
111
- it "should raise BadRequest when invalid params are passed in" do
112
- executing do
113
- DataCatalog::User.update(@user.id, { :garbage => "junk" })
114
- end.should raise_error(DataCatalog::BadRequest)
115
- end
116
-
117
- end # describe ".update"
118
-
119
- describe ".destroy" do
120
- before do
121
- @user = create_user
122
- end
123
-
124
- it "should destroy an existing user" do
125
- result = DataCatalog::User.destroy(@user.id)
126
- result.should be_true
127
- end
128
-
129
- it "should raise NotFound when non-existing user" do
130
- executing do
131
- DataCatalog::User.destroy(mangle(@user.id))
132
- end.should raise_error(DataCatalog::NotFound)
133
- end
134
- end # describe ".destroy"
135
-
136
- describe "#generate_api_key!" do
137
- before do
138
- @user = create_user
139
- end
140
-
141
- it "should generate a new key for the user" do
142
- @user.api_keys.length.should == 1
143
- @user.generate_api_key!({
144
- :purpose => "Civic hacking with my awesome app",
145
- :key_type => "application"
146
- }).should be_true
147
- @user.api_keys.length.should == 2
148
- @user.api_keys.last[:purpose].should == "Civic hacking with my awesome app"
149
- @user.application_api_keys.length.should == 1
150
- end
151
-
152
- it "should raise BadRequest when attempting to create a primary key" do
153
- executing do
154
- @user.generate_api_key!({
155
- :purpose => "Civic hacking with my awesome app",
156
- :key_type => "primary"
157
- })
158
- end.should raise_error(DataCatalog::BadRequest)
159
- end
160
- end # describe "#generate_api_key!"
161
-
162
- describe "#update_api_key!" do
163
- before do
164
- @user = create_user_with_2_keys
165
- end
166
-
167
- it "should update a key for the user" do
168
- @user.update_api_key!(@user.api_keys[1].id, {
169
- :key_type => "valet",
170
- :purpose => "To be more awesome"
171
- }).should be_true
172
- @user.api_keys[1].purpose.should == "To be more awesome"
173
- end
174
-
175
- it "should raise NotFound if updating a key that doesn't exist" do
176
- executing do
177
- @user.update_api_key!(mangle(@user.api_keys[1].id), {})
178
- end.should raise_error(DataCatalog::NotFound)
179
- end
180
-
181
- it "should raise BadRequest if primary key's type is changed" do
182
- executing do
183
- @user.update_api_key!(@user.api_keys[0].id, {
184
- :key_type => "valet"
185
- })
186
- end.should raise_error(DataCatalog::BadRequest)
187
- end
188
- end # describe "#update_api_key!"
189
-
190
- describe "#delete_api_key!" do
191
- before do
192
- @user = create_user_with_2_keys
193
- end
194
-
195
- it "should delete a key for the user" do
196
- @user.delete_api_key!(@user.api_keys[1].id).should be_true
197
- @user.api_keys.length.should == 1
198
- end
199
-
200
- it "should raise Conflict if deleting the primary key" do
201
- executing do
202
- @user.delete_api_key!(@user.api_keys[0].id)
203
- end.should raise_error(DataCatalog::Conflict)
204
- @user.api_keys.length.should == 2
205
- end
206
- end # describe "#delete_api_key!"
207
-
208
- 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
+ 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