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/api_key_spec.rb CHANGED
@@ -1,145 +1,145 @@
1
- require File.dirname(__FILE__) + '/spec_helper'
2
- include DataCatalog
3
-
4
- module ApiKeyHelpers
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
- ApiKey.create(user.id, {
15
- :purpose => "Civic hacking",
16
- :key_type => "application"
17
- })
18
- User.get(user.id)
19
- end
20
- end
21
-
22
- describe ApiKey do
23
- include ApiKeyHelpers
24
-
25
- before do
26
- setup_api
27
- clean_slate
28
- end
29
-
30
- describe ".all" do
31
- before do
32
- @user = create_user_with_2_keys
33
- @api_keys = ApiKey.all(@user.id)
34
- end
35
-
36
- it "should return an enumeration of API keys" do
37
- @api_keys.each do |api_key|
38
- api_key.should be_an_instance_of(ApiKey)
39
- end
40
- end
41
-
42
- it "should return correct titles" do
43
- @api_keys.map(&:key_type).should == %w(primary application)
44
- end
45
- end
46
-
47
- describe ".all with conditions" do
48
- before do
49
- @user = create_user_with_2_keys
50
- @api_keys = ApiKey.all(@user.id, :key_type => "application")
51
- end
52
-
53
- it "should return an enumeration of API keys" do
54
- @api_keys.each do |api_key|
55
- api_key.should be_an_instance_of(ApiKey)
56
- end
57
- end
58
-
59
- it "should return correct titles" do
60
- @api_keys.map(&:key_type).should == %w(application)
61
- end
62
- end
63
-
64
- describe ".create" do
65
- it "should create a new API key from basic params" do
66
- user = create_user_with_2_keys
67
- api_key = ApiKey.create(user.id, {
68
- :purpose => "Data wrangling",
69
- :key_type => "valet"
70
- })
71
- api_key.should be_an_instance_of(ApiKey)
72
- api_key.key_type.should == "valet"
73
- end
74
- end
75
-
76
- describe ".first" do
77
- before do
78
- @user = create_user_with_2_keys
79
- end
80
-
81
- it "should return an API key" do
82
- api_key = ApiKey.first(@user.id, :purpose => "Civic hacking")
83
- api_key.should be_an_instance_of(ApiKey)
84
- api_key.purpose.should == "Civic hacking"
85
- end
86
-
87
- it "should return nil if nothing found" do
88
- api_key = ApiKey.first(@user.id, :purpose => "Evil")
89
- api_key.should be_nil
90
- end
91
- end
92
-
93
- describe ".get" do
94
- before do
95
- @user = create_user_with_2_keys
96
- @api_key = @user.api_keys[1]
97
- end
98
-
99
- it "should return an API key" do
100
- api_key = ApiKey.get(@user.id, @api_key.id)
101
- api_key.should be_an_instance_of(ApiKey)
102
- api_key.purpose.should == "Civic hacking"
103
- end
104
-
105
- it "should raise NotFound if no API key exists" do
106
- executing do
107
- ApiKey.get(@user.id, mangle(@api_key.id))
108
- end.should raise_error(NotFound)
109
- end
110
- end
111
-
112
- describe ".update" do
113
- before do
114
- @user = create_user_with_2_keys
115
- @api_key = @user.api_keys[1]
116
- end
117
-
118
- it "should update an existing source from valid params" do
119
- api_key = ApiKey.update(@user.id, @api_key.id, {
120
- :purpose => "Local Government Reporting"
121
- })
122
- api_key.should be_an_instance_of(ApiKey)
123
- api_key.purpose.should == "Local Government Reporting"
124
- end
125
- end
126
-
127
- describe ".destroy" do
128
- before do
129
- @user = create_user_with_2_keys
130
- @api_key = @user.api_keys[1]
131
- end
132
-
133
- it "should destroy an existing source" do
134
- result = ApiKey.destroy(@user.id, @api_key.id)
135
- result.should be_true
136
- end
137
-
138
- it "should raise NotFound if API key does not exist" do
139
- executing do
140
- ApiKey.destroy(@user.id, mangle(@api_key.id))
141
- end.should raise_error(NotFound)
142
- end
143
- end
144
-
145
- end
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ include DataCatalog
3
+
4
+ module ApiKeyHelpers
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
+ ApiKey.create(user.id, {
15
+ :purpose => "Civic hacking",
16
+ :key_type => "application"
17
+ })
18
+ User.get(user.id)
19
+ end
20
+ end
21
+
22
+ describe ApiKey do
23
+ include ApiKeyHelpers
24
+
25
+ before do
26
+ setup_api
27
+ clean_slate
28
+ end
29
+
30
+ describe ".all" do
31
+ before do
32
+ @user = create_user_with_2_keys
33
+ @api_keys = ApiKey.all(@user.id)
34
+ end
35
+
36
+ it "should return an enumeration of API keys" do
37
+ @api_keys.each do |api_key|
38
+ api_key.should be_an_instance_of(ApiKey)
39
+ end
40
+ end
41
+
42
+ it "should return correct titles" do
43
+ @api_keys.map(&:key_type).should == %w(primary application)
44
+ end
45
+ end
46
+
47
+ describe ".all with conditions" do
48
+ before do
49
+ @user = create_user_with_2_keys
50
+ @api_keys = ApiKey.all(@user.id, :key_type => "application")
51
+ end
52
+
53
+ it "should return an enumeration of API keys" do
54
+ @api_keys.each do |api_key|
55
+ api_key.should be_an_instance_of(ApiKey)
56
+ end
57
+ end
58
+
59
+ it "should return correct titles" do
60
+ @api_keys.map(&:key_type).should == %w(application)
61
+ end
62
+ end
63
+
64
+ describe ".create" do
65
+ it "should create a new API key from basic params" do
66
+ user = create_user_with_2_keys
67
+ api_key = ApiKey.create(user.id, {
68
+ :purpose => "Data wrangling",
69
+ :key_type => "valet"
70
+ })
71
+ api_key.should be_an_instance_of(ApiKey)
72
+ api_key.key_type.should == "valet"
73
+ end
74
+ end
75
+
76
+ describe ".first" do
77
+ before do
78
+ @user = create_user_with_2_keys
79
+ end
80
+
81
+ it "should return an API key" do
82
+ api_key = ApiKey.first(@user.id, :purpose => "Civic hacking")
83
+ api_key.should be_an_instance_of(ApiKey)
84
+ api_key.purpose.should == "Civic hacking"
85
+ end
86
+
87
+ it "should return nil if nothing found" do
88
+ api_key = ApiKey.first(@user.id, :purpose => "Evil")
89
+ api_key.should be_nil
90
+ end
91
+ end
92
+
93
+ describe ".get" do
94
+ before do
95
+ @user = create_user_with_2_keys
96
+ @api_key = @user.api_keys[1]
97
+ end
98
+
99
+ it "should return an API key" do
100
+ api_key = ApiKey.get(@user.id, @api_key.id)
101
+ api_key.should be_an_instance_of(ApiKey)
102
+ api_key.purpose.should == "Civic hacking"
103
+ end
104
+
105
+ it "should raise NotFound if no API key exists" do
106
+ executing do
107
+ ApiKey.get(@user.id, mangle(@api_key.id))
108
+ end.should raise_error(NotFound)
109
+ end
110
+ end
111
+
112
+ describe ".update" do
113
+ before do
114
+ @user = create_user_with_2_keys
115
+ @api_key = @user.api_keys[1]
116
+ end
117
+
118
+ it "should update an existing source from valid params" do
119
+ api_key = ApiKey.update(@user.id, @api_key.id, {
120
+ :purpose => "Local Government Reporting"
121
+ })
122
+ api_key.should be_an_instance_of(ApiKey)
123
+ api_key.purpose.should == "Local Government Reporting"
124
+ end
125
+ end
126
+
127
+ describe ".destroy" do
128
+ before do
129
+ @user = create_user_with_2_keys
130
+ @api_key = @user.api_keys[1]
131
+ end
132
+
133
+ it "should destroy an existing source" do
134
+ result = ApiKey.destroy(@user.id, @api_key.id)
135
+ result.should be_true
136
+ end
137
+
138
+ it "should raise NotFound if API key does not exist" do
139
+ executing do
140
+ ApiKey.destroy(@user.id, mangle(@api_key.id))
141
+ end.should raise_error(NotFound)
142
+ end
143
+ end
144
+
145
+ end
data/spec/base_spec.rb CHANGED
@@ -1,129 +1,129 @@
1
- require File.dirname(__FILE__) + '/spec_helper'
2
- include DataCatalog
3
-
4
- describe Base do
5
-
6
- before do
7
- setup_api
8
- end
9
-
10
- describe ".base_uri=" do
11
- it "should set and normalize the base URI" do
12
- setup_api
13
- DataCatalog.base_uri = 'host.com'
14
- DataCatalog.base_uri.should == 'http://host.com'
15
- end
16
-
17
- it "should set the base URI to the default if it's not explicitly defined" do
18
- DataCatalog.base_uri = ''
19
- DataCatalog.base_uri.should == 'http://api.nationaldatacatalog.com'
20
- Base.base_uri.should == 'http://api.nationaldatacatalog.com'
21
- end
22
- end
23
-
24
- describe ".check_status" do
25
- it "should return nil on 200 OK" do
26
- response = HTTParty::Response.new(nil, '{"foo":"bar"}', 200, 'OK', {})
27
- Base.check_status(response).should be_nil
28
- end
29
-
30
- it "should raise BadRequest on 400 Bad Request" do
31
- response = HTTParty::Response.new(nil, '[]', 400, 'Bad Request', {})
32
- executing do
33
- Base.check_status(response)
34
- end.should raise_error(BadRequest)
35
- end
36
-
37
- it "should raise Unauthorized on 401 Unauthorized" do
38
- response = HTTParty::Response.new(nil, '', 401, 'Unauthorized', {})
39
- executing do
40
- Base.check_status(response)
41
- end.should raise_error(Unauthorized)
42
- end
43
-
44
- it "should raise NotFound on 404 Not Found" do
45
- response = HTTParty::Response.new(nil, '[]', 404, 'Not Found', {})
46
- executing do
47
- Base.check_status(response)
48
- end.should raise_error(NotFound)
49
- end
50
-
51
- it "should raise InternalServerError on 500 Internal Server Error" do
52
- response = HTTParty::Response.new(nil, '', 500, 'Internal Server Error', {})
53
- executing do
54
- Base.check_status(response)
55
- end.should raise_error(InternalServerError)
56
- end
57
- end
58
-
59
- describe ".error" do
60
- it "should return an 'Unable to parse:' message when body is blank" do
61
- response = HTTParty::Response.new(nil, '', 404, 'Not Found', {})
62
- Base.error(response).should == %(Unable to parse: "")
63
- end
64
-
65
- it "should return 'Response was empty' when body is an empty JSON object" do
66
- response = HTTParty::Response.new(nil, '{}', 404, 'Not Found', {})
67
- Base.error(response).should == "Response was empty"
68
- end
69
-
70
- it "should return 'Response was empty' when body is an empty array" do
71
- response = HTTParty::Response.new(nil, '[]', 404, 'Not Found', {})
72
- Base.error(response).should == "Response was empty"
73
- end
74
-
75
- it "should return the contents of the errors hash when it exists" do
76
- errors = '{"errors":["bad_error"]}'
77
- response = HTTParty::Response.new(nil, errors, 400, 'Bad Request', {})
78
- Base.error(response).should == '["bad_error"]'
79
- end
80
-
81
- it "should return the contents of the response body when no errors hash exists" do
82
- errors = '{"foo":["bar"]}'
83
- response = HTTParty::Response.new(nil, errors, 400, 'Bad Request', {})
84
- Base.error(response).should == '{"foo":["bar"]}'
85
- end
86
- end
87
-
88
- describe ".many" do
89
- it "should create an object from a filled array" do
90
- array = Base.many([
91
- {
92
- :name => "Carl Malamud",
93
- :email => "no-spam-carl@media.org"
94
- },
95
- {
96
- :name => "Ellen Miller",
97
- :email => "no-spam-ellen@sunlightfoundation.com"
98
- }
99
- ])
100
- array.map do |item|
101
- item.should be_an_instance_of(Base)
102
- end
103
- array.map(&:name).should == ["Carl Malamud", "Ellen Miller"]
104
- end
105
- end
106
-
107
- describe ".one" do
108
- it "should create an object from a filled hash" do
109
- hash = Base.one({
110
- :name => "John Smith",
111
- :email => "john@email.com"
112
- })
113
- hash.should be_an_instance_of(Base)
114
- hash.name.should == "John Smith"
115
- hash.email.should == "john@email.com"
116
- end
117
-
118
- it "should return nil from an empty hash" do
119
- hash = Base.one({})
120
- hash.should be_nil
121
- end
122
-
123
- it "should return nil from nil" do
124
- hash = Base.one(nil)
125
- hash.should be_nil
126
- end
127
- end
128
-
129
- end
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ include DataCatalog
3
+
4
+ describe Base do
5
+
6
+ before do
7
+ setup_api
8
+ end
9
+
10
+ describe ".base_uri=" do
11
+ it "should set and normalize the base URI" do
12
+ setup_api
13
+ DataCatalog.base_uri = 'host.com'
14
+ DataCatalog.base_uri.should == 'http://host.com'
15
+ end
16
+
17
+ it "should set the base URI to the default if it's not explicitly defined" do
18
+ DataCatalog.base_uri = ''
19
+ DataCatalog.base_uri.should == 'http://api.nationaldatacatalog.com'
20
+ Base.base_uri.should == 'http://api.nationaldatacatalog.com'
21
+ end
22
+ end
23
+
24
+ describe ".check_status" do
25
+ it "should return nil on 200 OK" do
26
+ response = HTTParty::Response.new(nil, '{"foo":"bar"}', 200, 'OK', {})
27
+ Base.check_status(response).should be_nil
28
+ end
29
+
30
+ it "should raise BadRequest on 400 Bad Request" do
31
+ response = HTTParty::Response.new(nil, '[]', 400, 'Bad Request', {})
32
+ executing do
33
+ Base.check_status(response)
34
+ end.should raise_error(BadRequest)
35
+ end
36
+
37
+ it "should raise Unauthorized on 401 Unauthorized" do
38
+ response = HTTParty::Response.new(nil, '', 401, 'Unauthorized', {})
39
+ executing do
40
+ Base.check_status(response)
41
+ end.should raise_error(Unauthorized)
42
+ end
43
+
44
+ it "should raise NotFound on 404 Not Found" do
45
+ response = HTTParty::Response.new(nil, '[]', 404, 'Not Found', {})
46
+ executing do
47
+ Base.check_status(response)
48
+ end.should raise_error(NotFound)
49
+ end
50
+
51
+ it "should raise InternalServerError on 500 Internal Server Error" do
52
+ response = HTTParty::Response.new(nil, '', 500, 'Internal Server Error', {})
53
+ executing do
54
+ Base.check_status(response)
55
+ end.should raise_error(InternalServerError)
56
+ end
57
+ end
58
+
59
+ describe ".error" do
60
+ it "should return an 'Unable to parse:' message when body is blank" do
61
+ response = HTTParty::Response.new(nil, '', 404, 'Not Found', {})
62
+ Base.error(response).should == %(Unable to parse: "")
63
+ end
64
+
65
+ it "should return 'Response was empty' when body is an empty JSON object" do
66
+ response = HTTParty::Response.new(nil, '{}', 404, 'Not Found', {})
67
+ Base.error(response).should == "Response was empty"
68
+ end
69
+
70
+ it "should return 'Response was empty' when body is an empty array" do
71
+ response = HTTParty::Response.new(nil, '[]', 404, 'Not Found', {})
72
+ Base.error(response).should == "Response was empty"
73
+ end
74
+
75
+ it "should return the contents of the errors hash when it exists" do
76
+ errors = '{"errors":["bad_error"]}'
77
+ response = HTTParty::Response.new(nil, errors, 400, 'Bad Request', {})
78
+ Base.error(response).should == '["bad_error"]'
79
+ end
80
+
81
+ it "should return the contents of the response body when no errors hash exists" do
82
+ errors = '{"foo":["bar"]}'
83
+ response = HTTParty::Response.new(nil, errors, 400, 'Bad Request', {})
84
+ Base.error(response).should == '{"foo":["bar"]}'
85
+ end
86
+ end
87
+
88
+ describe ".many" do
89
+ it "should create an object from a filled array" do
90
+ array = Base.many([
91
+ {
92
+ :name => "Carl Malamud",
93
+ :email => "no-spam-carl@media.org"
94
+ },
95
+ {
96
+ :name => "Ellen Miller",
97
+ :email => "no-spam-ellen@sunlightfoundation.com"
98
+ }
99
+ ])
100
+ array.map do |item|
101
+ item.should be_an_instance_of(Base)
102
+ end
103
+ array.map(&:name).should == ["Carl Malamud", "Ellen Miller"]
104
+ end
105
+ end
106
+
107
+ describe ".one" do
108
+ it "should create an object from a filled hash" do
109
+ hash = Base.one({
110
+ :name => "John Smith",
111
+ :email => "john@email.com"
112
+ })
113
+ hash.should be_an_instance_of(Base)
114
+ hash.name.should == "John Smith"
115
+ hash.email.should == "john@email.com"
116
+ end
117
+
118
+ it "should return nil from an empty hash" do
119
+ hash = Base.one({})
120
+ hash.should be_nil
121
+ end
122
+
123
+ it "should return nil from nil" do
124
+ hash = Base.one(nil)
125
+ hash.should be_nil
126
+ end
127
+ end
128
+
129
+ end