json_voorhees 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (210) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +75 -0
  4. data/Rakefile +34 -0
  5. data/app/assets/javascripts/json_voorhees/application.js +13 -0
  6. data/app/assets/stylesheets/json_voorhees/application.css +15 -0
  7. data/app/controllers/json_voorhees/application_controller.rb +4 -0
  8. data/app/helpers/json_voorhees/application_helper.rb +4 -0
  9. data/app/views/layouts/json_voorhees/application.html.erb +14 -0
  10. data/config/routes.rb +2 -0
  11. data/lib/generators/json_voorhees/app_make_authorizations/USAGE +10 -0
  12. data/lib/generators/json_voorhees/app_make_authorizations/app_make_authorizations_generator.rb +39 -0
  13. data/lib/generators/json_voorhees/app_make_authorizations/templates/auth_file.rb.erb +51 -0
  14. data/lib/generators/json_voorhees/app_make_tests/USAGE +9 -0
  15. data/lib/generators/json_voorhees/app_make_tests/app_make_tests_generator.rb +51 -0
  16. data/lib/generators/json_voorhees/app_make_tests/templates/factory.rb.erb +9 -0
  17. data/lib/generators/json_voorhees/app_make_tests/templates/model.rb.erb +30 -0
  18. data/lib/generators/json_voorhees/app_make_tests/templates/request.rb.erb +102 -0
  19. data/lib/generators/json_voorhees/app_make_tests/templates/routing.rb.erb +47 -0
  20. data/lib/generators/json_voorhees/engine_create_controller/USAGE +12 -0
  21. data/lib/generators/json_voorhees/engine_create_controller/engine_create_controller_generator.rb +40 -0
  22. data/lib/generators/json_voorhees/engine_create_controller/templates/controller_template.rb.erb +98 -0
  23. data/lib/generators/json_voorhees/engine_create_controller/templates/no_auth_controller_template.rb.erb +61 -0
  24. data/lib/generators/json_voorhees/engine_create_serializer/USAGE +9 -0
  25. data/lib/generators/json_voorhees/engine_create_serializer/engine_create_serializer_generator.rb +34 -0
  26. data/lib/generators/json_voorhees/engine_create_serializer/templates/serializer.rb.erb +23 -0
  27. data/lib/generators/json_voorhees/setup_app/USAGE +9 -0
  28. data/lib/generators/json_voorhees/setup_app/setup_app_generator.rb +207 -0
  29. data/lib/generators/json_voorhees/setup_app/templates/api_controller_no_arcadex.rb +2 -0
  30. data/lib/generators/json_voorhees/setup_app/templates/api_controller_with_arcadex.rb +26 -0
  31. data/lib/generators/json_voorhees/setup_app/templates/app_index_controller.rb +4 -0
  32. data/lib/generators/json_voorhees/setup_app/templates/hidden_rspec.rb +3 -0
  33. data/lib/generators/json_voorhees/setup_app/templates/json_helpers.rb +7 -0
  34. data/lib/generators/json_voorhees/setup_app/templates/rspec_factory_girl.rb +16 -0
  35. data/lib/generators/json_voorhees/setup_app/templates/user/include_authorization.rb +3 -0
  36. data/lib/generators/json_voorhees/setup_app/templates/user/specs/factory_girl.rb +14 -0
  37. data/lib/generators/json_voorhees/setup_app/templates/user/specs/model_specs.rb +59 -0
  38. data/lib/generators/json_voorhees/setup_app/templates/user/specs/request_specs.rb +154 -0
  39. data/lib/generators/json_voorhees/setup_app/templates/user/specs/route_specs.rb +77 -0
  40. data/lib/generators/json_voorhees/setup_app/templates/user/user_authorizations.rb +82 -0
  41. data/lib/generators/json_voorhees/setup_app/templates/user/user_controller.rb +124 -0
  42. data/lib/generators/json_voorhees/setup_app/templates/user/user_model.rb +26 -0
  43. data/lib/generators/json_voorhees/setup_app/templates/user/user_routes.rb +24 -0
  44. data/lib/generators/json_voorhees/setup_app/templates/user/user_serializer.rb +39 -0
  45. data/lib/generators/json_voorhees/setup_app/templates/views/admin_no_user +6 -0
  46. data/lib/generators/json_voorhees/setup_app/templates/views/admin_with_user +9 -0
  47. data/lib/generators/json_voorhees/setup_app/templates/views/app +0 -0
  48. data/lib/generators/json_voorhees/setup_app/templates/views/app_index +9 -0
  49. data/lib/generators/json_voorhees/setup_app/templates/views/application +19 -0
  50. data/lib/generators/json_voorhees/setup_engine/USAGE +8 -0
  51. data/lib/generators/json_voorhees/setup_engine/setup_engine_generator.rb +81 -0
  52. data/lib/generators/json_voorhees/setup_engine/templates/api_controller.rb.erb +5 -0
  53. data/lib/generators/json_voorhees/setup_engine/templates/application_controller.rb.erb +4 -0
  54. data/lib/json_voorhees/engine.rb +5 -0
  55. data/lib/json_voorhees/version.rb +3 -0
  56. data/lib/json_voorhees.rb +4 -0
  57. data/lib/tasks/json_voorhees_tasks.rake +4 -0
  58. data/test/dummy/README.rdoc +28 -0
  59. data/test/dummy/Rakefile +6 -0
  60. data/test/dummy/app/assets/javascripts/application.js +13 -0
  61. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  62. data/test/dummy/app/controllers/application_controller.rb +5 -0
  63. data/test/dummy/app/helpers/application_helper.rb +2 -0
  64. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  65. data/test/dummy/bin/bundle +3 -0
  66. data/test/dummy/bin/rails +4 -0
  67. data/test/dummy/bin/rake +4 -0
  68. data/test/dummy/config/application.rb +23 -0
  69. data/test/dummy/config/boot.rb +5 -0
  70. data/test/dummy/config/database.yml +25 -0
  71. data/test/dummy/config/environment.rb +5 -0
  72. data/test/dummy/config/environments/development.rb +37 -0
  73. data/test/dummy/config/environments/production.rb +82 -0
  74. data/test/dummy/config/environments/test.rb +39 -0
  75. data/test/dummy/config/initializers/assets.rb +8 -0
  76. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  77. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  78. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  79. data/test/dummy/config/initializers/inflections.rb +16 -0
  80. data/test/dummy/config/initializers/mime_types.rb +4 -0
  81. data/test/dummy/config/initializers/session_store.rb +3 -0
  82. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  83. data/test/dummy/config/locales/en.yml +23 -0
  84. data/test/dummy/config/routes.rb +4 -0
  85. data/test/dummy/config/secrets.yml +22 -0
  86. data/test/dummy/config.ru +4 -0
  87. data/test/dummy/log/development.log +0 -0
  88. data/test/dummy/public/404.html +67 -0
  89. data/test/dummy/public/422.html +67 -0
  90. data/test/dummy/public/500.html +66 -0
  91. data/test/dummy/public/favicon.ico +0 -0
  92. data/test/integration/navigation_test.rb +10 -0
  93. data/test/json_voorhees_test.rb +7 -0
  94. data/test/lib/generators/json_voorhees/engine_serializer_generator_test.rb +16 -0
  95. data/test/test_app/Gemfile +42 -0
  96. data/test/test_app/Gemfile.lock +132 -0
  97. data/test/test_app/README.rdoc +28 -0
  98. data/test/test_app/Rakefile +6 -0
  99. data/test/test_app/app/assets/javascripts/application.js +16 -0
  100. data/test/test_app/app/assets/stylesheets/application.css +15 -0
  101. data/test/test_app/app/controllers/application_controller.rb +5 -0
  102. data/test/test_app/app/helpers/application_helper.rb +2 -0
  103. data/test/test_app/app/views/layouts/application.html.erb +14 -0
  104. data/test/test_app/bin/bundle +3 -0
  105. data/test/test_app/bin/rails +8 -0
  106. data/test/test_app/bin/rake +8 -0
  107. data/test/test_app/bin/spring +18 -0
  108. data/test/test_app/config/application.rb +23 -0
  109. data/test/test_app/config/boot.rb +4 -0
  110. data/test/test_app/config/database.yml +25 -0
  111. data/test/test_app/config/environment.rb +5 -0
  112. data/test/test_app/config/environments/development.rb +37 -0
  113. data/test/test_app/config/environments/production.rb +82 -0
  114. data/test/test_app/config/environments/test.rb +39 -0
  115. data/test/test_app/config/initializers/assets.rb +8 -0
  116. data/test/test_app/config/initializers/backtrace_silencers.rb +7 -0
  117. data/test/test_app/config/initializers/cookies_serializer.rb +3 -0
  118. data/test/test_app/config/initializers/filter_parameter_logging.rb +4 -0
  119. data/test/test_app/config/initializers/inflections.rb +16 -0
  120. data/test/test_app/config/initializers/mime_types.rb +4 -0
  121. data/test/test_app/config/initializers/session_store.rb +3 -0
  122. data/test/test_app/config/initializers/wrap_parameters.rb +14 -0
  123. data/test/test_app/config/locales/en.yml +23 -0
  124. data/test/test_app/config/routes.rb +56 -0
  125. data/test/test_app/config/secrets.yml +22 -0
  126. data/test/test_app/config.ru +4 -0
  127. data/test/test_app/db/development.sqlite3 +0 -0
  128. data/test/test_app/db/schema.rb +16 -0
  129. data/test/test_app/db/seeds.rb +7 -0
  130. data/test/test_app/log/development.log +1517 -0
  131. data/test/test_app/log/production.log +68 -0
  132. data/test/test_app/log/test.log +10482 -0
  133. data/test/test_app/public/404.html +67 -0
  134. data/test/test_app/public/422.html +67 -0
  135. data/test/test_app/public/500.html +66 -0
  136. data/test/test_app/public/favicon.ico +0 -0
  137. data/test/test_app/public/robots.txt +5 -0
  138. data/test/test_app/test/test_helper.rb +10 -0
  139. data/test/test_app/tmp/cache/assets/development/sprockets/055b16b21481c5603eab51f88bce614e +0 -0
  140. data/test/test_app/tmp/cache/assets/development/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
  141. data/test/test_app/tmp/cache/assets/development/sprockets/2c8821d40541c62f65d0a77d6d737a59 +0 -0
  142. data/test/test_app/tmp/cache/assets/development/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
  143. data/test/test_app/tmp/cache/assets/development/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
  144. data/test/test_app/tmp/cache/assets/development/sprockets/381d73d62d2e34310ff5e02a60e2e6b5 +0 -0
  145. data/test/test_app/tmp/cache/assets/development/sprockets/6cfd8687a851913292b638daebcf0c3b +0 -0
  146. data/test/test_app/tmp/cache/assets/development/sprockets/72539eb249ff535aa9679adf93578a3a +0 -0
  147. data/test/test_app/tmp/cache/assets/development/sprockets/7288979cc8fd497d915d19732930197f +0 -0
  148. data/test/test_app/tmp/cache/assets/development/sprockets/af6f30b38676f9ac87b6c15260d702d6 +0 -0
  149. data/test/test_app/tmp/cache/assets/development/sprockets/c1e06c98ebb85a26421cf34d75787365 +0 -0
  150. data/test/test_app/tmp/cache/assets/development/sprockets/c636869dd0df678775f14b9e86cb147d +0 -0
  151. data/test/test_app/tmp/cache/assets/development/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
  152. data/test/test_app/tmp/cache/assets/development/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
  153. data/test/test_app/tmp/cache/assets/development/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
  154. data/test/test_engine/Gemfile +17 -0
  155. data/test/test_engine/Gemfile.lock +128 -0
  156. data/test/test_engine/MIT-LICENSE +20 -0
  157. data/test/test_engine/README.md +1 -0
  158. data/test/test_engine/Rakefile +34 -0
  159. data/test/test_engine/app/assets/javascripts/test_engine/application.js +13 -0
  160. data/test/test_engine/app/assets/stylesheets/test_engine/application.css +15 -0
  161. data/test/test_engine/app/controllers/test_engine/api/v1/application_controller.rb +5 -0
  162. data/test/test_engine/app/controllers/test_engine/application_controller.rb +4 -0
  163. data/test/test_engine/app/helpers/test_engine/application_helper.rb +4 -0
  164. data/test/test_engine/app/views/layouts/test_engine/default/application.html.erb +14 -0
  165. data/test/test_engine/bin/rails +12 -0
  166. data/test/test_engine/config/routes.rb +23 -0
  167. data/test/test_engine/lib/tasks/test_engine_tasks.rake +4 -0
  168. data/test/test_engine/lib/test_engine/engine.rb +5 -0
  169. data/test/test_engine/lib/test_engine/version.rb +3 -0
  170. data/test/test_engine/lib/test_engine.rb +4 -0
  171. data/test/test_engine/test/dummy/README.rdoc +28 -0
  172. data/test/test_engine/test/dummy/Rakefile +6 -0
  173. data/test/test_engine/test/dummy/app/assets/javascripts/application.js +13 -0
  174. data/test/test_engine/test/dummy/app/assets/stylesheets/application.css +15 -0
  175. data/test/test_engine/test/dummy/app/controllers/application_controller.rb +5 -0
  176. data/test/test_engine/test/dummy/app/helpers/application_helper.rb +2 -0
  177. data/test/test_engine/test/dummy/app/views/layouts/application.html.erb +14 -0
  178. data/test/test_engine/test/dummy/bin/bundle +3 -0
  179. data/test/test_engine/test/dummy/bin/rails +4 -0
  180. data/test/test_engine/test/dummy/bin/rake +4 -0
  181. data/test/test_engine/test/dummy/config/application.rb +23 -0
  182. data/test/test_engine/test/dummy/config/boot.rb +5 -0
  183. data/test/test_engine/test/dummy/config/database.yml +25 -0
  184. data/test/test_engine/test/dummy/config/environment.rb +5 -0
  185. data/test/test_engine/test/dummy/config/environments/development.rb +37 -0
  186. data/test/test_engine/test/dummy/config/environments/production.rb +82 -0
  187. data/test/test_engine/test/dummy/config/environments/test.rb +39 -0
  188. data/test/test_engine/test/dummy/config/initializers/assets.rb +8 -0
  189. data/test/test_engine/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  190. data/test/test_engine/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  191. data/test/test_engine/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  192. data/test/test_engine/test/dummy/config/initializers/inflections.rb +16 -0
  193. data/test/test_engine/test/dummy/config/initializers/mime_types.rb +4 -0
  194. data/test/test_engine/test/dummy/config/initializers/session_store.rb +3 -0
  195. data/test/test_engine/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  196. data/test/test_engine/test/dummy/config/locales/en.yml +23 -0
  197. data/test/test_engine/test/dummy/config/routes.rb +4 -0
  198. data/test/test_engine/test/dummy/config/secrets.yml +22 -0
  199. data/test/test_engine/test/dummy/config.ru +4 -0
  200. data/test/test_engine/test/dummy/log/development.log +0 -0
  201. data/test/test_engine/test/dummy/public/404.html +67 -0
  202. data/test/test_engine/test/dummy/public/422.html +67 -0
  203. data/test/test_engine/test/dummy/public/500.html +66 -0
  204. data/test/test_engine/test/dummy/public/favicon.ico +0 -0
  205. data/test/test_engine/test/integration/navigation_test.rb +10 -0
  206. data/test/test_engine/test/test_engine_test.rb +7 -0
  207. data/test/test_engine/test/test_helper.rb +15 -0
  208. data/test/test_engine/test_engine.gemspec +31 -0
  209. data/test/test_helper.rb +15 -0
  210. metadata +434 -0
@@ -0,0 +1,154 @@
1
+ require "rails_helper"
2
+
3
+ RSpec.describe ::People::User, :type => :request do
4
+ describe "sign up process" do
5
+ before(:example) do
6
+ @attrs = FactoryGirl.attributes_for(:people_user)
7
+ end
8
+ # post /api/1/users/register
9
+ it "checks response of a register request with a valid user object" do
10
+ post 'api/1/users/register', :user => @attrs
11
+ expect(response.status).to eq(200) #ok
12
+ expect(::People::User.all.count).to eq(1)
13
+ #A valid token needs to be returned
14
+ expect(json["token"]["auth_token"]).to_not eq(nil)
15
+ token = ::People::User.find(1).tokens[0]
16
+ expect(json["token"]["auth_token"]).to eq(token.auth_token)
17
+ end
18
+ # post /api/1/users/login
19
+ it "checks response of a register request with mismatched passwords" do
20
+ @attrs["password"] = "password1"
21
+ @attrs["password_confirmation"] = "password2"
22
+ post 'api/1/users/register', :user => @attrs
23
+ expect(response.status).to eq(422) #invalid_resource
24
+ expect(::People::User.all.count).to eq(0)
25
+ #Errors need to be returned
26
+ expect(json["errors"]).to_not eq(nil)
27
+ end
28
+ # post /api/1/users/login
29
+ it "checks response of a valid login request" do
30
+ @attrs["password"] = "password123"
31
+ @attrs["password_confirmation"] = "password123"
32
+ user = FactoryGirl.create(:people_user,@attrs)
33
+ old_auth_token = user.tokens[0].auth_token
34
+ #It needs to send an email and password
35
+ post 'api/1/users/login', @attrs
36
+ expect(response.status).to eq(200) #ok
37
+ #A valid and new token need to be returned
38
+ expect(json["token"]["auth_token"]).to_not eq(old_auth_token)
39
+ #This token should be the newest token in the database
40
+ new_auth_token = ::People::User.find(1).tokens.order("created_at").last.auth_token
41
+ expect(json["token"]["auth_token"]).to eq(new_auth_token)
42
+ end
43
+ # post /api/1/users/login
44
+ it "checks response of an invalid login request with an invalid password" do
45
+ @attrs["password"] = "password123"
46
+ @attrs["password_confirmation"] = "password123"
47
+ user = FactoryGirl.create(:people_user,@attrs)
48
+ #The password needs to be invalid
49
+ @attrs["password"] = "wrongPassword"
50
+ #It needs to send an email and password
51
+ post 'api/1/users/login', @attrs
52
+ expect(response.status).to eq(401) #unauthorized
53
+ #Errors need to be returned
54
+ expect(json["errors"]).to_not eq(nil)
55
+ end
56
+ # get /api/1/users/1/logout
57
+ it "checks logout response and makes sure token is deleted" do
58
+ user = FactoryGirl.create(:people_user,@attrs)
59
+ token = user.tokens[0].auth_token
60
+ header = {"Auth-Token" => token, "Email" => user.email}
61
+ get 'api/1/users/logout', nil, header
62
+ expect(::People::User.count).to eq(1)
63
+ expect(response.status).to eq(200) #ok
64
+ expect(::Arcadex::Token.count).to eq(0)
65
+ end
66
+ end
67
+ end
68
+ RSpec.describe ::People::User, :type => :request do
69
+ describe "Rest Routes" do
70
+ before(:example) do
71
+ @user = FactoryGirl.create(:people_user)
72
+ token = @user.tokens[0].auth_token
73
+ @header = {"Auth-Token" => token, "Email" => @user.email}
74
+ end
75
+ # get /api/1/users
76
+ it "checks index json response" do
77
+ FactoryGirl.create_list(:people_user, 10)
78
+ get 'api/1/users', nil, @header
79
+ expect(response.status).to eq(200) #ok
80
+ expect(json["users"]).to_not eq(nil)
81
+ end
82
+ # get /api/1/users/1
83
+ it "checks show json response" do
84
+ get 'api/1/users/1', nil, @header
85
+ expect(response.status).to eq(200) #ok
86
+ expect(json["user"]["id"]).to eq(1)
87
+ end
88
+ it "checks for 404 response" do
89
+ get 'api/1/users/20', nil, @header
90
+ expect(response.status).to eq(404) #not_found
91
+ end
92
+ # put /api/1/users/1
93
+ it "checks update json response" do
94
+ attrs = FactoryGirl.attributes_for(:people_user)
95
+ attrs["email"] = "Cole@LaunchU.com"
96
+ hash = {"user" => attrs}
97
+ #This uses users 1 header
98
+ put 'api/1/users/1', hash, @header
99
+ expect(response.status).to eq(200) #ok
100
+ expect(json["user"]["email"]).to eq("Cole@LaunchU.com")
101
+ end
102
+ end
103
+ end
104
+ RSpec.describe ::People::User, :type => :request do
105
+ describe "Authorization" do
106
+ before(:example) do
107
+ @user = FactoryGirl.create(:people_user)
108
+ token = @user.tokens[0].auth_token
109
+ @header = {"Auth-Token" => token, "Email" => @user.email}
110
+ end
111
+ # put /api/1/users/1
112
+ it "makes sure a user cannot alter another users information" do
113
+ FactoryGirl.create(:people_user)
114
+ attrs = FactoryGirl.attributes_for(:people_user)
115
+ attrs["email"] = "Cole@LaunchU.com"
116
+ hash = {"user" => attrs}
117
+ #This uses users 2 header
118
+ put 'api/1/users/2', hash, @header
119
+ expect(response.status).to eq(403) #forbidden
120
+ end
121
+ it "makes sure a user can't update to bad values" do
122
+ attrs = FactoryGirl.attributes_for(:people_user)
123
+ attrs["email"] = ""
124
+ hash = {"user" => attrs}
125
+ #This uses users 1 header
126
+ put 'api/1/users/1', hash, @header
127
+ expect(response.status).to eq(422) #ok
128
+ end
129
+ end
130
+ end
131
+ RSpec.describe ::People::User, :type => :request do
132
+ describe "Serialization" do
133
+ before(:example) do
134
+ @user = FactoryGirl.create(:people_user)
135
+ token = @user.tokens[0].auth_token
136
+ @header = {"Auth-Token" => token, "Email" => @user.email}
137
+ end
138
+ # get /api/1/users
139
+ it "checks only appropriate attributes are sent back for index" do
140
+ FactoryGirl.create_list(:people_user, 10)
141
+ get 'api/1/users', nil, @header
142
+ expect(response.status).to eq(200) #ok
143
+ expect(::People::User.count).to eq(11)
144
+ expect(::Arcadex::Token.count).to eq(11)
145
+ expect(json["users"][0]["password_digest"]).to eq(nil)
146
+ expect(json["users"][0]["created_at"]).to_not eq(nil)
147
+ expect(json["users"][0]["updated_at"]).to_not eq(nil)
148
+ end
149
+ it "checks the show json sent back" do
150
+ get 'api/1/users/1', nil, @header
151
+ expect(response.status).to eq(200) #ok
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,77 @@
1
+ require "rails_helper"
2
+
3
+ #The register, login, and logout routes
4
+ RSpec.describe "Users sign up process routing", :type => :routing do
5
+ routes { People::Engine.routes }
6
+
7
+ it "routes to register" do
8
+ expect(:post => "/api/1/users/register").to route_to(
9
+ :controller => "people/api/v1/users",
10
+ :action => "register"
11
+ )
12
+ end
13
+ it "routes to login" do
14
+ expect(:post => "/api/1/users/login").to route_to(
15
+ :controller => "people/api/v1/users",
16
+ :action => "login"
17
+ )
18
+ end
19
+ it "routes to logout" do
20
+ expect(:get => "/api/1/users/logout").to route_to(
21
+ :controller => "people/api/v1/users",
22
+ :action => "logout"
23
+ )
24
+ end
25
+ end
26
+ #The standard rest routes for the user controller
27
+ RSpec.describe "Users controller rest routing", :type => :routing do
28
+ routes { People::Engine.routes }
29
+
30
+ it "routes to index" do
31
+ expect(:get => "/api/1/users").to route_to(
32
+ :controller => "people/api/v1/users",
33
+ :action => "index"
34
+ )
35
+ end
36
+ it "routes to show" do
37
+ expect(:get => "/api/1/users/1").to route_to(
38
+ :controller => "people/api/v1/users",
39
+ :action => "show",
40
+ :id => "1"
41
+ )
42
+ end
43
+ it "routes to edit" do
44
+ expect(:get => "/api/1/users/1/edit").to route_to(
45
+ :controller => "people/api/v1/users",
46
+ :action => "edit",
47
+ :id => "1"
48
+ )
49
+ end
50
+ it "routes to create" do
51
+ expect(:post => "/api/1/users/").to route_to(
52
+ :controller => "people/api/v1/users",
53
+ :action => "create"
54
+ )
55
+ end
56
+ it "routes to update" do
57
+ expect(:put => "/api/1/users/1/").to route_to(
58
+ :controller => "people/api/v1/users",
59
+ :action => "update",
60
+ :id => "1"
61
+ )
62
+ end
63
+ it "routes to update" do
64
+ expect(:patch => "/api/1/users/1/").to route_to(
65
+ :controller => "people/api/v1/users",
66
+ :action => "update",
67
+ :id => "1"
68
+ )
69
+ end
70
+ it "routes to delete" do
71
+ expect(:delete => "/api/1/users/1/").to route_to(
72
+ :controller => "people/api/v1/users",
73
+ :action => "destroy",
74
+ :id => "1"
75
+ )
76
+ end
77
+ end
@@ -0,0 +1,82 @@
1
+ module Authorization
2
+ module People
3
+ module User
4
+
5
+ #Used in the controller
6
+
7
+ def self.register?
8
+ return true
9
+ end
10
+
11
+ def self.login?
12
+ return true
13
+ end
14
+
15
+ def self.logout?(tokenUser)
16
+ return true
17
+ end
18
+
19
+ def self.index?(tokenUser)
20
+ return true
21
+ end
22
+
23
+ def self.show?(targetUser,tokenUser)
24
+ return true
25
+ end
26
+
27
+
28
+ def self.update?(targetUser,tokenUser)
29
+ if targetUser != tokenUser
30
+ #Can only update your own data
31
+ return false
32
+ else
33
+ return true
34
+ end
35
+ end
36
+
37
+ #Used in the serializer, current_user may be nil
38
+
39
+ def self.include_id?(current_user,user_object,options)
40
+ action = options[:url_options][:_recall][:action]
41
+ controller = options[:url_options][:_recall][:controller]
42
+ return true
43
+ end
44
+
45
+ def self.include_email?(current_user,user_object,options)
46
+ action = options[:url_options][:_recall][:action]
47
+ controller = options[:url_options][:_recall][:controller]
48
+ return true
49
+ end
50
+
51
+ def self.include_username?(current_user,user_object,options)
52
+ action = options[:url_options][:_recall][:action]
53
+ controller = options[:url_options][:_recall][:controller]
54
+ return true
55
+ end
56
+
57
+ def self.include_created_at?(current_user,user_object,options)
58
+ action = options[:url_options][:_recall][:action]
59
+ controller = options[:url_options][:_recall][:controller]
60
+ return true
61
+ end
62
+
63
+ def self.include_updated_at?(current_user,user_object,options)
64
+ action = options[:url_options][:_recall][:action]
65
+ controller = options[:url_options][:_recall][:controller]
66
+ return true
67
+ end
68
+
69
+ def self.include_tokens?(current_user,user_object,options)
70
+ action = options[:url_options][:_recall][:action]
71
+ controller = options[:url_options][:_recall][:controller]
72
+ #if action == "index" && controller == "people/api/v1/users"
73
+ #return false
74
+ #end
75
+ return false
76
+ end
77
+
78
+ private
79
+
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,124 @@
1
+ require_dependency "people/api/v1/application_controller"
2
+ require 'authorization'
3
+
4
+ module People
5
+ class Api::V1::UsersController < Api::V1::ApplicationController
6
+ skip_before_filter :authenticate_user, :only => [:register, :login]
7
+ before_action :set_user, only: [:show, :edit, :update]
8
+ before_action :register_authorize, only: [:register]
9
+ before_action :login_authorize, only: [:login]
10
+ before_action :logout_authorize, only: [:logout]
11
+ before_action :index_authorize, only: [:index]
12
+ before_action :show_authorize, only: [:show]
13
+ before_action :update_authorize, only: [:update]
14
+
15
+ # POST /api/1/users/register
16
+ def register
17
+ #Create a new user
18
+ user = ::People::User.new(user_params)
19
+ if user.save
20
+ #If the user is saved, return a token
21
+ token = user.tokens[0]
22
+ render :json => {user: user, token: token}
23
+ else
24
+ #Return an error if not saved
25
+ render :json => {errors: user.errors}, status: :unprocessable_entity
26
+ end
27
+ end
28
+
29
+ # POST /api/1/users/login
30
+ def login
31
+ #Should I delete the current token or ignore it?
32
+ #Find user from email and password. Create and return a new token
33
+ user = ::People::User.find_by(email: params[:email])
34
+ if user && user.authenticate(params[:password])
35
+ token = user.tokens.create
36
+ render :json => {user: user, token: token}
37
+ else
38
+ render :json => {errors: "Email and/or Password is incorrect"}, status: :unauthorized
39
+ end
40
+ end
41
+
42
+ # GET /api/1/users/logout
43
+ def logout
44
+ #Destroy the current token
45
+ token = current_token
46
+ token.destroy
47
+ render json: {}
48
+ end
49
+
50
+ # GET /api/1/users
51
+ def index
52
+ @users = ::People::User.all
53
+ render json: @users
54
+ end
55
+
56
+ # GET /api/1/users/1
57
+ def show
58
+ render json: @user
59
+ end
60
+
61
+ # PATCH/PUT /api/1/users/1
62
+ def update
63
+ if @user.update(user_params)
64
+ render json: @user
65
+ else
66
+ render :json => {errors: @user.errors}, status: :unprocessable_entity
67
+ end
68
+ end
69
+
70
+ private
71
+ # Use callbacks to share common setup or constraints between actions.
72
+
73
+ def set_user
74
+ @user = ::People::User.find_by_id(params[:id])
75
+ if @user.nil?
76
+ render :json => {errors: "User was not found"}, status: :not_found
77
+ end
78
+ end
79
+
80
+ # Only allow a trusted parameter "white list" through.
81
+
82
+ def user_params
83
+ params.require(:user).permit(:username, :email, :password, :password_confirmation)
84
+ end
85
+
86
+ # Authorizations below here
87
+
88
+ def register_authorize
89
+ if !::Authorization::People::User.register?
90
+ render :json => {errors: "User is not authorized for this action"}, status: :forbidden
91
+ end
92
+ end
93
+
94
+ def login_authorize
95
+ if !::Authorization::People::User.login?
96
+ render :json => {errors: "User is not authorized for this action"}, status: :forbidden
97
+ end
98
+ end
99
+
100
+ def logout_authorize
101
+ if !::Authorization::People::User.logout?(current_user)
102
+ render :json => {errors: "User is not authorized for this action"}, status: :forbidden
103
+ end
104
+ end
105
+
106
+ def index_authorize
107
+ if !::Authorization::People::User.index?(current_user)
108
+ render :json => {errors: "User is not authorized for this action"}, status: :forbidden
109
+ end
110
+ end
111
+
112
+ def show_authorize
113
+ if !::Authorization::People::User.show?(@user,current_user)
114
+ render :json => {errors: "User is not authorized for this action"}, status: :forbidden
115
+ end
116
+ end
117
+
118
+ def update_authorize
119
+ if !::Authorization::People::User.update?(@user,current_user)
120
+ render :json => {errors: "User is not authorized for this action"}, status: :forbidden
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,26 @@
1
+ require 'type_cartographer'
2
+
3
+ module People
4
+ class User < ActiveRecord::Base
5
+
6
+ has_many :tokens, :as => :imageable, :class_name => "::Arcadex::Token"
7
+
8
+ has_secure_password
9
+
10
+ after_create :setup_user
11
+
12
+ validates :username, presence: true
13
+ #validates_format_of :username, :with => /\A[A-Za-z0-9\d]+\Z/i
14
+ validates :email, presence: true
15
+ #Might need a regex for emails, or just rather confirm them
16
+
17
+ def setup_user
18
+ create_token
19
+ end
20
+
21
+ def create_token
22
+ self.tokens.create!
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,24 @@
1
+ People::Engine.routes.draw do
2
+
3
+ root to: "users#index"
4
+
5
+ scope 'admin' do
6
+ resources :users
7
+ end
8
+
9
+ scope 'api' do
10
+ scope '1' do
11
+ resources :users, controller: 'api/v1/users' do
12
+ collection do
13
+ # /api/1/users/register
14
+ post 'register', to: "api/v1/users#register"
15
+ # /api/1/users/login
16
+ post 'login', to: "api/v1/users#login"
17
+ # /api/1/users/logout
18
+ get 'logout', to: "api/v1/users#logout"
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ end
@@ -0,0 +1,39 @@
1
+ require 'authorization'
2
+
3
+ module People
4
+ class UserSerializer < ActiveModel::Serializer
5
+ embed :ids, include: true
6
+
7
+ attributes :id
8
+ attributes :username
9
+ attributes :email
10
+ attributes :created_at
11
+ attributes :updated_at
12
+ has_many :tokens
13
+
14
+ def include_id?
15
+ return ::Authorization::People::User.include_id?(current_user,object,@options)
16
+ end
17
+
18
+ def include_email?
19
+ return ::Authorization::People::User.include_email?(current_user,object,@options)
20
+ end
21
+
22
+ def include_username?
23
+ return ::Authorization::People::User.include_username?(current_user,object,@options)
24
+ end
25
+
26
+ def include_created_at?
27
+ return ::Authorization::People::User.include_created_at?(current_user,object,@options)
28
+ end
29
+
30
+ def include_updated_at?
31
+ return ::Authorization::People::User.include_updated_at?(current_user,object,@options)
32
+ end
33
+
34
+ def include_associations!
35
+ include! :tokens if ::Authorization::People::User.include_tokens?(current_user,object,@options)
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,6 @@
1
+ <h1>ADMIN</h1>
2
+
3
+ <ul>
4
+ <li>Engines</li>
5
+
6
+ </ul>
@@ -0,0 +1,9 @@
1
+ <h1>ADMIN</h1>
2
+
3
+ <ul>
4
+ <li>People Engine</li>
5
+ <li><%= link_to 'user_index', people.users_path %></li>
6
+
7
+ <li>Arcadex Engine</li>
8
+ <li><%= link_to 'token_index', arcadex.tokens_path %></li>
9
+ </ul>
@@ -0,0 +1,9 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title></title>
5
+ </head>
6
+ <body>
7
+ <%= yield %>
8
+ </body>
9
+ </html>
@@ -0,0 +1,19 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title></title>
5
+ <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
6
+ <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ <% if !current_page?(main_app.root_url) %>
14
+ <br><br>
15
+ <%= link_to 'admin_home', main_app.admin_path %>
16
+ <% end %>
17
+
18
+ </body>
19
+ </html>
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Sets up an engine with commonly used files and boilerplate code.
3
+
4
+ Example:
5
+ rails g json_voorhees:setup_engine People
6
+
7
+ This will create:
8
+ A lot of boilerplate
@@ -0,0 +1,81 @@
1
+ module JsonVoorhees
2
+ class SetupEngineGenerator < Rails::Generators::Base
3
+ source_root File.expand_path('../templates', __FILE__)
4
+ argument :module_name, :type => :string
5
+
6
+ def sprint
7
+ generate_layout
8
+ create_api_controller
9
+ add_gems
10
+ setup_routes
11
+ setup_application_controller
12
+ run_bundle
13
+ end
14
+
15
+ private
16
+
17
+ def setup_application_controller
18
+ run "rm -f app/controllers/#{module_name.underscore}/application_controller.rb"
19
+ template "application_controller.rb.erb", "app/controllers/#{module_name.underscore}/application_controller.rb"
20
+ end
21
+
22
+ def run_bundle
23
+ run "bundle update"
24
+ run "bundle install"
25
+ end
26
+
27
+ def setup_routes
28
+ inject_into_file "config/routes.rb", after: "#{module_name.camelize}::Engine.routes.draw do\n" do <<-'RUBY'
29
+
30
+ #root to: "controller_name#index"
31
+ scope 'admin' do
32
+
33
+ end
34
+
35
+ scope 'api' do
36
+ scope '1' do
37
+ #/api/1/controller_name
38
+ #resources :controller_name, controller: 'api/v1/controller_name'
39
+ #resources :controller_name2, controller: 'api/v1/controller_name2' do
40
+ #collection do
41
+ #/api/1/controller_name2/action
42
+ #post 'action', to: "api/v1/controller_name2#action"
43
+ #end
44
+ #end
45
+ end
46
+ end
47
+
48
+ #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-
49
+
50
+ RUBY
51
+ end
52
+ end
53
+
54
+ def add_gems
55
+ inject_into_file "#{module_name.downcase}.gemspec", after: "s.test_files = Dir[\"test/**/*\"]\n" do <<-'RUBY'
56
+
57
+ s.add_dependency "rails-api"
58
+ s.add_dependency "type_cartographer"
59
+ s.add_dependency "arcadex"
60
+ s.add_dependency "active_model_serializers", "~> 0.8.0"
61
+ s.add_dependency "bcrypt", "~> 3.1.7"
62
+ s.add_development_dependency "json_voorhees"
63
+ s.add_development_dependency "annotate", ">=2.6.0"
64
+
65
+ RUBY
66
+ end
67
+ end
68
+
69
+ def create_api_controller
70
+ template "api_controller.rb.erb", "app/controllers/#{module_name.underscore}/api/v1/application_controller.rb"
71
+ end
72
+
73
+ def generate_layout
74
+ run "mkdir app/views/layouts/#{module_name.downcase}/default/"
75
+ run "mv app/views/layouts/#{module_name.downcase}/application.html.erb app/views/layouts/#{module_name.downcase}/default/application.html.erb"
76
+ run "mkdir app/serializers"
77
+ run "mkdir app/serializers/#{module_name.downcase} && touch app/serializers/#{module_name.downcase}/.gitkeep"
78
+ run "rm -f README.rdoc && echo '# Describe your engine here' > README.md"
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,5 @@
1
+ module <%= module_name.camelize %>
2
+ class Api::V1::ApplicationController < ::Api::V1::ApiController
3
+
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ module <%= module_name.camelize %>
2
+ class ApplicationController < ::ActionController::Base
3
+ end
4
+ end