introspective_grape 0.5.7 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +22 -18
  3. data/README.md +2 -0
  4. data/introspective_grape.gemspec +5 -4
  5. data/lib/introspective_grape/api.rb +461 -459
  6. data/lib/introspective_grape/create_helpers.rb +25 -25
  7. data/lib/introspective_grape/traversal.rb +1 -1
  8. data/lib/introspective_grape/validators.rb +36 -36
  9. data/lib/introspective_grape/version.rb +5 -5
  10. data/spec/dummy/Gemfile +23 -22
  11. data/spec/dummy/app/api/api_helpers.rb +38 -37
  12. data/spec/dummy/app/api/dummy_api.rb +61 -61
  13. data/spec/dummy/app/api/permissions_helper.rb +7 -7
  14. data/spec/dummy/app/helpers/current.rb +3 -0
  15. data/spec/dummy/app/models/chat_message.rb +34 -34
  16. data/spec/dummy/app/models/chat_user.rb +16 -16
  17. data/spec/dummy/app/models/location.rb +26 -26
  18. data/spec/dummy/app/models/role.rb +30 -30
  19. data/spec/dummy/app/models/team.rb +14 -9
  20. data/spec/dummy/app/models/user/chatter.rb +79 -79
  21. data/spec/dummy/bin/bundle +3 -3
  22. data/spec/dummy/bin/rails +4 -4
  23. data/spec/dummy/bin/setup +36 -29
  24. data/spec/dummy/bin/update +31 -0
  25. data/spec/dummy/bin/yarn +11 -0
  26. data/spec/dummy/config/application.rb +30 -37
  27. data/spec/dummy/config/boot.rb +3 -6
  28. data/spec/dummy/config/environment.rb +5 -11
  29. data/spec/dummy/config/environments/development.rb +54 -42
  30. data/spec/dummy/config/environments/production.rb +81 -79
  31. data/spec/dummy/config/environments/test.rb +47 -44
  32. data/spec/dummy/config/initializers/application_controller_renderer.rb +8 -0
  33. data/spec/dummy/config/initializers/assets.rb +14 -11
  34. data/spec/dummy/config/initializers/content_security_policy.rb +25 -0
  35. data/spec/dummy/config/initializers/cookies_serializer.rb +5 -3
  36. data/spec/dummy/config/initializers/inflections.rb +16 -16
  37. data/spec/dummy/config/initializers/new_framework_defaults_5_2.rb +38 -0
  38. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -14
  39. data/spec/dummy/config/locales/en.yml +33 -23
  40. data/spec/dummy/config/storage.yml +34 -0
  41. data/spec/models/image_spec.rb +10 -14
  42. data/spec/requests/project_api_spec.rb +185 -182
  43. data/spec/requests/user_api_spec.rb +221 -221
  44. data/spec/support/request_helpers.rb +22 -21
  45. metadata +20 -157
@@ -1,182 +1,185 @@
1
- require 'rails_helper'
2
-
3
- describe Dummy::ProjectAPI, type: :request do
4
- before :all do
5
- [User,Project,Company,Location].map(&:destroy_all)
6
- cm = User.make!(email:'company.admin@springshot.com')
7
- pm = User.make!(email:'project.admin@springshot.com')
8
-
9
- 2.times { Project.make! }
10
-
11
- c = Company.make!(name:"Sprockets")
12
- p = Project.make!(name:"Manufacture Sprockets", owner: c)
13
- Project.make!(name:"Disassemble Sprockets", owner: c)
14
-
15
- cm.admin_companies.push c
16
- pm.admin_projects.push p
17
-
18
- cm.save!
19
- pm.save!
20
- end
21
-
22
- let(:company) { Company.find_by_name("Sprockets") }
23
- let(:project) { Project.find_by_name("Manufacture Sprockets") }
24
-
25
- context "As a super admin" do
26
- it "should return a list of all projects" do
27
- get '/api/v1/projects', params: { per_page: 10, offset: 0 }
28
- response.should be_successful
29
- json.length.should == Project.count
30
- json.map{|c| c['id'].to_i}.include?(project.id).should == true
31
- end
32
-
33
- it "should return the specified project" do
34
- get "/api/v1/projects/#{project.id}"
35
- response.should be_successful
36
- json['name'].should == project.name
37
- end
38
-
39
- it "should return an error if the project doesn't exist" do
40
- get "/api/v1/projects/#{Project.last.id+1}"
41
- response.code.should == "404"
42
- end
43
-
44
- context "edit a project team" do
45
-
46
- before(:each) do
47
- @team = Team.make!(project: project)
48
- @u1 = User.make!
49
- @u2 = User.make!
50
- UserProjectJob.make!(project: project, job: project.jobs.first, user: @u1)
51
- UserProjectJob.make!(project: project, job: project.jobs.first, user: @u2)
52
- end
53
-
54
- context "via nested attributes" do
55
- it "should create a team with users" do
56
- p = {
57
- name: 'New Team', team_users_attributes: [{ user_id: @u1.id }, { user_id: @u2.id }]
58
- }
59
- post "/api/v1/projects/#{project.id}/teams", params: p
60
- response.should be_successful
61
- Team.last.name.should == 'New Team'
62
- Team.last.users.to_a.should == [@u1,@u2]
63
- end
64
-
65
- it "should add a team member" do
66
- p = { team_users_attributes: [
67
- { user_id: @u1.id }, { user_id: @u2.id }
68
- ] }
69
- put "/api/v1/projects/#{project.id}/teams/#{@team.id}", params: p
70
- response.should be_successful
71
-
72
- Team.last.users.to_a.should == [@u1,@u2]
73
- end
74
-
75
- it "should delete a team member" do
76
- @team.users << [@u1,@u2]
77
- @team.save!
78
- p = { team_users_attributes: [
79
- { id: @team.team_users.where(user_id:@u1.id).first.id, _destroy: 1 }
80
- ] }
81
- put "/api/v1/projects/#{project.id}/teams/#{@team.id}", params: p
82
- response.should be_successful
83
- Team.last.users.to_a.should == [@u2]
84
- end
85
- end
86
-
87
- context "edit a project team via nested routes" do
88
- it "should add a team member" do
89
- p = { user_id: @u1.id }
90
- post "/api/v1/projects/#{project.id}/teams/#{@team.id}/team_users", params: p
91
- response.should be_successful
92
- Team.last.users.to_a.should == [@u1]
93
- end
94
-
95
- it "should delete a team member" do
96
- @team.users << [@u1,@u2]
97
- @team.save!
98
- id = @team.team_users.where(user_id:@u1.id).first.id
99
- delete "/api/v1/projects/#{project.id}/teams/#{@team.id}/team_users/#{id}"
100
- response.should be_successful
101
- Team.last.users.to_a.should == [@u2]
102
- end
103
- end
104
- end
105
- end
106
-
107
- context "As a company admin" do
108
- before :all do
109
- @without_authentication = true
110
- end
111
-
112
- before :each do
113
- Grape::Endpoint.before_each do |endpoint|
114
- allow(endpoint).to receive(:current_user) do
115
- User.find_by_email("company.admin@springshot.com")
116
- end
117
- end
118
- end
119
-
120
- it "should return a list of all the company's projects" do
121
- get '/api/v1/projects', params: { offset: 0 }
122
- response.should be_successful
123
- json.length.should == 2
124
- json.map{|c| c['name']}.include?("Manufacture Sprockets").should == true
125
- json.map{|c| c['name']}.include?("Disassemble Sprockets").should == true
126
- end
127
-
128
- end
129
-
130
- context "As a project admin" do
131
- before :all do
132
- @without_authentication = true
133
- end
134
- before :each do
135
- Grape::Endpoint.before_each do |endpoint|
136
- allow(endpoint).to receive(:current_user) do
137
- User.find_by_email("project.admin@springshot.com")
138
- end
139
- end
140
- end
141
-
142
- it "should return a list of all the project admin's projects" do
143
- get '/api/v1/projects', params: { offset: 0 }
144
- response.should be_successful
145
- json.length.should == 1
146
- json.map{|c| c['name']}.include?("Manufacture Sprockets").should == true
147
- json.map{|c| c['name']}.include?("Disassemble Sprockets").should == false
148
- end
149
- end
150
-
151
- context :pagination do
152
- before(:all) do
153
- Project.destroy_all
154
- 20.times { Project.make! }
155
- end
156
-
157
- it "should return the project API's declared default paginated results" do
158
- get '/api/v1/projects'
159
- response.should be_successful
160
- json.length.should == 2
161
- json.first['id'].should eq Project.all[2].id
162
- json.second['id'].should eq Project.all[3].id
163
- response.headers.slice("X-Total", "X-Total-Pages", "X-Per-Page", "X-Page", "X-Next-Page", "X-Prev-Page", "X-Offset").values.should eq ["20", "9", "2", "1", "2", "", "2"]
164
- end
165
-
166
- it "should return the request number of results" do
167
- get '/api/v1/projects', params: { per_page: 9, offset: 9 }
168
- response.should be_successful
169
- json.size.should == 9
170
- json.map {|j| j['id']}.should eq Project.all[9..17].map(&:id)
171
- response.headers.slice("X-Total", "X-Total-Pages", "X-Per-Page", "X-Page", "X-Next-Page", "X-Prev-Page", "X-Offset").values.should eq ["20", "2", "9", "1", "2", "", "9"]
172
- end
173
-
174
- it "should respect the maximum number of results" do
175
- get '/api/v1/projects', params: { per_page: 20, offset: 0 }
176
- response.code.should eq "400"
177
- json['error'].should eq "per_page must be less than or equal 10"
178
- end
179
- end
180
-
181
-
182
- end
1
+ require 'rails_helper'
2
+
3
+ describe Dummy::ProjectAPI, type: :request do
4
+ before :all do
5
+ [User,Project,Company,Location].map(&:destroy_all)
6
+ cm = User.make!(email:'company.admin@springshot.com')
7
+ pm = User.make!(email:'project.admin@springshot.com')
8
+
9
+ 2.times { Project.make! }
10
+
11
+ c = Company.make!(name:"Sprockets")
12
+ p = Project.make!(name:"Manufacture Sprockets", owner: c)
13
+ Project.make!(name:"Disassemble Sprockets", owner: c)
14
+
15
+ cm.admin_companies.push c
16
+ pm.admin_projects.push p
17
+
18
+ cm.save!
19
+ pm.save!
20
+ end
21
+
22
+ let(:company) { Company.find_by_name("Sprockets") }
23
+ let(:project) { Project.find_by_name("Manufacture Sprockets") }
24
+
25
+ context "As a super admin" do
26
+ it "should return a list of all projects" do
27
+ get '/api/v1/projects', params: { per_page: 10, offset: 0 }
28
+ response.should be_successful
29
+ json.length.should == Project.count
30
+ json.map{|c| c['id'].to_i}.include?(project.id).should == true
31
+ end
32
+
33
+ it "should return the specified project" do
34
+ get "/api/v1/projects/#{project.id}"
35
+ response.should be_successful
36
+ json['name'].should == project.name
37
+ end
38
+
39
+ it "should return an error if the project doesn't exist" do
40
+ get "/api/v1/projects/#{Project.last.id+1}"
41
+ response.code.should == "404"
42
+ end
43
+
44
+ context "edit a project team" do
45
+
46
+ before(:each) do
47
+ @team = Team.make!(project: project)
48
+ @u1 = User.make!
49
+ @u2 = User.make!
50
+ UserProjectJob.make!(project: project, job: project.jobs.first, user: @u1)
51
+ UserProjectJob.make!(project: project, job: project.jobs.first, user: @u2)
52
+ end
53
+
54
+ context "via nested attributes" do
55
+ it "should create a team with users" do
56
+ count = Team.count
57
+ p = {
58
+ name: 'New Team', creator_id: @u1.id, team_users_attributes: [{ user_id: @u1.id }, { user_id: @u2.id }]
59
+ }
60
+ post "/api/v1/projects/#{project.id}/teams", params: p
61
+ response.should be_successful
62
+
63
+ Team.count.should eq count+1
64
+ Team.last.name.should == 'New Team'
65
+ Team.last.users.to_a.should == [@u1,@u2]
66
+ end
67
+
68
+ it "should add a team member" do
69
+ p = { team_users_attributes: [
70
+ { user_id: @u1.id }, { user_id: @u2.id }
71
+ ] }
72
+ put "/api/v1/projects/#{project.id}/teams/#{@team.id}", params: p
73
+ response.should be_successful
74
+
75
+ Team.last.users.to_a.should == [@u1,@u2]
76
+ end
77
+
78
+ it "should delete a team member" do
79
+ @team.users << [@u1,@u2]
80
+ @team.save!
81
+ p = { team_users_attributes: [
82
+ { id: @team.team_users.where(user_id:@u1.id).first.id, _destroy: 1 }
83
+ ] }
84
+ put "/api/v1/projects/#{project.id}/teams/#{@team.id}", params: p
85
+ response.should be_successful
86
+ Team.last.users.to_a.should == [@u2]
87
+ end
88
+ end
89
+
90
+ context "edit a project team via nested routes" do
91
+ it "should add a team member" do
92
+ p = { user_id: @u1.id }
93
+ post "/api/v1/projects/#{project.id}/teams/#{@team.id}/team_users", params: p
94
+ response.should be_successful
95
+ Team.last.users.to_a.should == [@u1]
96
+ end
97
+
98
+ it "should delete a team member" do
99
+ @team.users << [@u1,@u2]
100
+ @team.save!
101
+ id = @team.team_users.where(user_id:@u1.id).first.id
102
+ delete "/api/v1/projects/#{project.id}/teams/#{@team.id}/team_users/#{id}"
103
+ response.should be_successful
104
+ Team.last.users.to_a.should == [@u2]
105
+ end
106
+ end
107
+ end
108
+ end
109
+
110
+ context "As a company admin" do
111
+ before :all do
112
+ @without_authentication = true
113
+ end
114
+
115
+ before :each do
116
+ Grape::Endpoint.before_each do |endpoint|
117
+ allow(endpoint).to receive(:current_user) do
118
+ User.find_by_email("company.admin@springshot.com")
119
+ end
120
+ end
121
+ end
122
+
123
+ it "should return a list of all the company's projects" do
124
+ get '/api/v1/projects', params: { offset: 0 }
125
+ response.should be_successful
126
+ json.length.should == 2
127
+ json.map{|c| c['name']}.include?("Manufacture Sprockets").should == true
128
+ json.map{|c| c['name']}.include?("Disassemble Sprockets").should == true
129
+ end
130
+
131
+ end
132
+
133
+ context "As a project admin" do
134
+ before :all do
135
+ @without_authentication = true
136
+ end
137
+ before :each do
138
+ Grape::Endpoint.before_each do |endpoint|
139
+ allow(endpoint).to receive(:current_user) do
140
+ User.find_by_email("project.admin@springshot.com")
141
+ end
142
+ end
143
+ end
144
+
145
+ it "should return a list of all the project admin's projects" do
146
+ get '/api/v1/projects', params: { offset: 0 }
147
+ response.should be_successful
148
+ json.length.should == 1
149
+ json.map{|c| c['name']}.include?("Manufacture Sprockets").should == true
150
+ json.map{|c| c['name']}.include?("Disassemble Sprockets").should == false
151
+ end
152
+ end
153
+
154
+ context :pagination do
155
+ before(:all) do
156
+ Project.destroy_all
157
+ 20.times { Project.make! }
158
+ end
159
+
160
+ it "should return the project API's declared default paginated results" do
161
+ get '/api/v1/projects'
162
+ response.should be_successful
163
+ json.length.should == 2
164
+ json.first['id'].should eq Project.all[2].id
165
+ json.second['id'].should eq Project.all[3].id
166
+ response.headers.slice("X-Total", "X-Total-Pages", "X-Per-Page", "X-Page", "X-Next-Page", "X-Prev-Page", "X-Offset").values.should eq ["20", "9", "2", "1", "2", "", "2"]
167
+ end
168
+
169
+ it "should return the request number of results" do
170
+ get '/api/v1/projects', params: { per_page: 9, offset: 9 }
171
+ response.should be_successful
172
+ json.size.should == 9
173
+ json.map {|j| j['id']}.should eq Project.all[9..17].map(&:id)
174
+ response.headers.slice("X-Total", "X-Total-Pages", "X-Per-Page", "X-Page", "X-Next-Page", "X-Prev-Page", "X-Offset").values.should eq ["20", "2", "9", "1", "2", "", "9"]
175
+ end
176
+
177
+ it "should respect the maximum number of results" do
178
+ get '/api/v1/projects', params: { per_page: 20, offset: 0 }
179
+ response.code.should eq "400"
180
+ json['error'].should eq "per_page must be less than or equal 10"
181
+ end
182
+ end
183
+
184
+
185
+ end