curupira 0.1.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of curupira might be problematic. Click here for more details.

Files changed (181) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +19 -0
  5. data/Gemfile +17 -0
  6. data/Gemfile.lock +202 -0
  7. data/README.md +72 -0
  8. data/Rakefile +31 -0
  9. data/app/controllers/curupira/authorized_controller.rb +9 -0
  10. data/app/controllers/curupira/groups_controller.rb +43 -0
  11. data/app/controllers/curupira/passwords_controller.rb +46 -0
  12. data/app/controllers/curupira/roles_controller.rb +43 -0
  13. data/app/controllers/curupira/sessions_controller.rb +30 -0
  14. data/app/controllers/curupira/users_controller.rb +47 -0
  15. data/app/helpers/curupira/authorize_helper.rb +21 -0
  16. data/app/helpers/curupira/relationship_select_options_helper.rb +17 -0
  17. data/app/mailers/curupira/reset_password_mailer.rb +13 -0
  18. data/app/views/curupira/groups/_form.html.erb +28 -0
  19. data/app/views/curupira/groups/_role_group_fields.html.erb +9 -0
  20. data/app/views/curupira/groups/edit.html.erb +3 -0
  21. data/app/views/curupira/groups/index.html.erb +24 -0
  22. data/app/views/curupira/groups/new.html.erb +3 -0
  23. data/app/views/curupira/groups/show.html.erb +32 -0
  24. data/app/views/curupira/passwords/edit.html.erb +14 -0
  25. data/app/views/curupira/passwords/new.html.erb +14 -0
  26. data/app/views/curupira/reset_password_mailer/instructions.html.erb +19 -0
  27. data/app/views/curupira/reset_password_mailer/reseted.html.erb +7 -0
  28. data/app/views/curupira/roles/_authorization_fields.html.erb +10 -0
  29. data/app/views/curupira/roles/_form.html.erb +23 -0
  30. data/app/views/curupira/roles/edit.html.erb +3 -0
  31. data/app/views/curupira/roles/index.html.erb +24 -0
  32. data/app/views/curupira/roles/new.html.erb +3 -0
  33. data/app/views/curupira/roles/show.html.erb +37 -0
  34. data/app/views/curupira/sessions/new.html.erb +17 -0
  35. data/app/views/curupira/shared/_model_links.html.erb +7 -0
  36. data/app/views/curupira/shared/_session_links.html.erb +7 -0
  37. data/app/views/curupira/users/_form.html.erb +43 -0
  38. data/app/views/curupira/users/_group_user_fields.html.erb +20 -0
  39. data/app/views/curupira/users/_role_group_user_fields.html.erb +10 -0
  40. data/app/views/curupira/users/edit.html.erb +3 -0
  41. data/app/views/curupira/users/index.html.erb +28 -0
  42. data/app/views/curupira/users/new.html.erb +3 -0
  43. data/app/views/curupira/users/show.html.erb +28 -0
  44. data/curupira.gemspec +28 -0
  45. data/lib/curupira.rb +16 -0
  46. data/lib/curupira/authorizer.rb +43 -0
  47. data/lib/curupira/rails.rb +30 -0
  48. data/lib/curupira/rails/routes.rb +15 -0
  49. data/lib/curupira/version.rb +3 -0
  50. data/lib/generators/curupira/controllers/controllers_generator.rb +27 -0
  51. data/lib/generators/curupira/install/install_generator.rb +80 -0
  52. data/lib/generators/curupira/install/model_generators_helper.rb +266 -0
  53. data/lib/generators/curupira/routes/routes_generator.rb +21 -0
  54. data/lib/generators/curupira/routes/templates/routes.rb +1 -0
  55. data/lib/generators/curupira/templates/db/migrate/add_curupira_to_users.rb +20 -0
  56. data/lib/generators/curupira/templates/db/migrate/create_action_labels.rb +12 -0
  57. data/lib/generators/curupira/templates/db/migrate/create_authorizations.rb +12 -0
  58. data/lib/generators/curupira/templates/db/migrate/create_features.rb +11 -0
  59. data/lib/generators/curupira/templates/db/migrate/create_group_users.rb +13 -0
  60. data/lib/generators/curupira/templates/db/migrate/create_groups.rb +10 -0
  61. data/lib/generators/curupira/templates/db/migrate/create_role_group_users.rb +12 -0
  62. data/lib/generators/curupira/templates/db/migrate/create_role_groups.rb +13 -0
  63. data/lib/generators/curupira/templates/db/migrate/create_roles.rb +8 -0
  64. data/lib/generators/curupira/templates/db/migrate/sorcery_core.rb +27 -0
  65. data/lib/generators/curupira/templates/models/action_label.rb +2 -0
  66. data/lib/generators/curupira/templates/models/authorization.rb +2 -0
  67. data/lib/generators/curupira/templates/models/feature.rb +2 -0
  68. data/lib/generators/curupira/templates/models/feature_service.rb +4 -0
  69. data/lib/generators/curupira/templates/models/group.rb +2 -0
  70. data/lib/generators/curupira/templates/models/group_user.rb +2 -0
  71. data/lib/generators/curupira/templates/models/role.rb +2 -0
  72. data/lib/generators/curupira/templates/models/role_group.rb +2 -0
  73. data/lib/generators/curupira/templates/models/role_group_user.rb +2 -0
  74. data/lib/generators/curupira/templates/models/service.rb +2 -0
  75. data/lib/generators/curupira/templates/models/user.rb +2 -0
  76. data/lib/generators/curupira/templates/sorcery.rb +462 -0
  77. data/lib/generators/curupira/views/views_generator.rb +27 -0
  78. data/lib/tasks/curupira_tasks.rake +4 -0
  79. data/spec/app_templates/app/models/action_label.rb +2 -0
  80. data/spec/app_templates/app/models/authorization.rb +2 -0
  81. data/spec/app_templates/app/models/feature.rb +2 -0
  82. data/spec/app_templates/app/models/feature_service.rb +2 -0
  83. data/spec/app_templates/app/models/group.rb +4 -0
  84. data/spec/app_templates/app/models/group_user.rb +2 -0
  85. data/spec/app_templates/app/models/role.rb +2 -0
  86. data/spec/app_templates/app/models/role_group.rb +2 -0
  87. data/spec/app_templates/app/models/role_group_user.rb +2 -0
  88. data/spec/app_templates/app/models/service.rb +2 -0
  89. data/spec/app_templates/app/models/user.rb +2 -0
  90. data/spec/app_templates/config/routes.rb +3 -0
  91. data/spec/controllers/controller_spec.rb +40 -0
  92. data/spec/controllers/groups_controller_spec.rb +162 -0
  93. data/spec/controllers/passwords_controller_spec.rb +171 -0
  94. data/spec/controllers/roles_controller_spec.rb +168 -0
  95. data/spec/controllers/sessions_controller_spec.rb +112 -0
  96. data/spec/controllers/users_controller_spec.rb +182 -0
  97. data/spec/curupira/authorization_spec.rb +67 -0
  98. data/spec/dummy/README.rdoc +28 -0
  99. data/spec/dummy/Rakefile +6 -0
  100. data/spec/dummy/app/assets/images/.keep +0 -0
  101. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  102. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  103. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  104. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  105. data/spec/dummy/app/controllers/home_controller.rb +4 -0
  106. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  107. data/spec/dummy/app/mailers/.keep +0 -0
  108. data/spec/dummy/app/models/.keep +0 -0
  109. data/spec/dummy/app/models/action_label.rb +3 -0
  110. data/spec/dummy/app/models/authorization.rb +4 -0
  111. data/spec/dummy/app/models/feature.rb +3 -0
  112. data/spec/dummy/app/models/group.rb +9 -0
  113. data/spec/dummy/app/models/group_user.rb +7 -0
  114. data/spec/dummy/app/models/role.rb +10 -0
  115. data/spec/dummy/app/models/role_group.rb +4 -0
  116. data/spec/dummy/app/models/role_group_user.rb +4 -0
  117. data/spec/dummy/app/models/user.rb +10 -0
  118. data/spec/dummy/app/views/home/index.html.erb +5 -0
  119. data/spec/dummy/app/views/home/test.html.erb +1 -0
  120. data/spec/dummy/app/views/layouts/application.html.erb +19 -0
  121. data/spec/dummy/bin/bundle +3 -0
  122. data/spec/dummy/bin/rails +4 -0
  123. data/spec/dummy/bin/rake +4 -0
  124. data/spec/dummy/bin/setup +29 -0
  125. data/spec/dummy/config.ru +4 -0
  126. data/spec/dummy/config/application.rb +25 -0
  127. data/spec/dummy/config/boot.rb +5 -0
  128. data/spec/dummy/config/database.yml +25 -0
  129. data/spec/dummy/config/environment.rb +5 -0
  130. data/spec/dummy/config/environments/development.rb +46 -0
  131. data/spec/dummy/config/environments/production.rb +79 -0
  132. data/spec/dummy/config/environments/test.rb +44 -0
  133. data/spec/dummy/config/initializers/assets.rb +11 -0
  134. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  135. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  136. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  137. data/spec/dummy/config/initializers/inflections.rb +16 -0
  138. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  139. data/spec/dummy/config/initializers/session_store.rb +3 -0
  140. data/spec/dummy/config/initializers/sorcery.rb +462 -0
  141. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  142. data/spec/dummy/config/locales/en.yml +23 -0
  143. data/spec/dummy/config/routes.rb +6 -0
  144. data/spec/dummy/config/secrets.yml +22 -0
  145. data/spec/dummy/db/migrate/20150305011941_create_role_group_users.rb +12 -0
  146. data/spec/dummy/db/migrate/20150305011942_create_authorizations.rb +12 -0
  147. data/spec/dummy/db/migrate/20150305011944_create_groups.rb +10 -0
  148. data/spec/dummy/db/migrate/20150305011945_create_roles.rb +8 -0
  149. data/spec/dummy/db/migrate/20150305011946_sorcery_core.rb +27 -0
  150. data/spec/dummy/db/migrate/20150305011947_create_group_users.rb +13 -0
  151. data/spec/dummy/db/migrate/20150305011948_create_role_groups.rb +13 -0
  152. data/spec/dummy/db/migrate/20150312181229_create_action_labels.rb +12 -0
  153. data/spec/dummy/db/migrate/20150312181747_create_features.rb +11 -0
  154. data/spec/dummy/db/schema.rb +112 -0
  155. data/spec/dummy/db/seeds.rb +137 -0
  156. data/spec/dummy/lib/assets/.keep +0 -0
  157. data/spec/dummy/log/.keep +0 -0
  158. data/spec/dummy/public/404.html +67 -0
  159. data/spec/dummy/public/422.html +67 -0
  160. data/spec/dummy/public/500.html +66 -0
  161. data/spec/dummy/public/favicon.ico +0 -0
  162. data/spec/features/link_authorize_spec.rb +25 -0
  163. data/spec/features/list_users_spec.rb +38 -0
  164. data/spec/features/log_in_spec.rb +15 -0
  165. data/spec/generators/curupira/install/action_label_spec.rb +53 -0
  166. data/spec/generators/curupira/install/authorization_generator_spec.rb +51 -0
  167. data/spec/generators/curupira/install/feature_generator_spec.rb +52 -0
  168. data/spec/generators/curupira/install/group_user_generator_spec.rb +51 -0
  169. data/spec/generators/curupira/install/install_generators_spec.rb +159 -0
  170. data/spec/generators/curupira/install/role_generator_spec.rb +38 -0
  171. data/spec/generators/curupira/install/role_group_generator_spec.rb +51 -0
  172. data/spec/generators/curupira/install/role_group_user_generator_spec.rb +52 -0
  173. data/spec/helpers/relationship_select_options_helper_spec.rb +18 -0
  174. data/spec/rails_helper.rb +55 -0
  175. data/spec/spec_helper.rb +87 -0
  176. data/spec/support/factories.rb +64 -0
  177. data/spec/support/generator_spec_helpers.rb +42 -0
  178. data/spec/support/helpers_spec.rb +23 -0
  179. data/spec/support/shared_examples/role_generator_shared_example_spec.rb +13 -0
  180. data/spec/support/sorcery.rb +4 -0
  181. metadata +539 -0
@@ -0,0 +1,168 @@
1
+ require 'rails_helper'
2
+
3
+ describe Curupira::RolesController do
4
+ let(:user) { FactoryGirl.create :user }
5
+
6
+ before do
7
+ login_user(user)
8
+ end
9
+
10
+ describe "GET index" do
11
+ let!(:roles) { FactoryGirl.create_list(:role, 2) }
12
+
13
+ before do
14
+ get :index
15
+ end
16
+
17
+ it { expect(response).to be_success }
18
+
19
+ it { expect(assigns(:roles).count).to eql 2 }
20
+ end
21
+
22
+ describe "GET show" do
23
+ context "when role exists" do
24
+ let!(:role) { FactoryGirl.create(:role) }
25
+
26
+ it "should get show" do
27
+ get :show, id: role
28
+ expect(response).to be_success
29
+ end
30
+
31
+ it "returns role" do
32
+ get :show, id: user
33
+ expect(assigns(:role)).to eql role
34
+ end
35
+ end
36
+
37
+ context "when user does not exist" do
38
+ it "renders 404" do
39
+ expect {
40
+ get :show, id: "wrong id"
41
+ }.to raise_error ActiveRecord::RecordNotFound
42
+ end
43
+ end
44
+ end
45
+
46
+ describe "GET new" do
47
+ it "should get new" do
48
+ get :new
49
+ expect(response).to be_success
50
+ end
51
+
52
+ it "returns new role" do
53
+ get :new
54
+ expect(assigns(:role)).to be_new_record
55
+ end
56
+ end
57
+
58
+ describe "GET edit" do
59
+ context "when role exists" do
60
+ let!(:role) { FactoryGirl.create(:role) }
61
+
62
+ it "should get edit" do
63
+ get :edit, id: role
64
+ expect(response).to be_success
65
+ end
66
+
67
+ it "returns role" do
68
+ get :edit, id: role
69
+ expect(assigns(:role)).to eql role
70
+ end
71
+ end
72
+
73
+ context "when user does not exist" do
74
+ it "renders 404" do
75
+ expect {
76
+ get :edit, id: "wrong id"
77
+ }.to raise_error ActiveRecord::RecordNotFound
78
+ end
79
+ end
80
+ end
81
+
82
+ describe "POST create" do
83
+ context "when role is valid" do
84
+ let!(:feature) { FactoryGirl.create(:feature, name: "Cadastrar") }
85
+ let(:params) do
86
+ {
87
+ name: "Minha Role",
88
+ authorizations_attributes: {
89
+ "0": {
90
+ feature_id: feature.id
91
+ }
92
+ }
93
+ }
94
+ end
95
+
96
+ it "should redirect to new user" do
97
+ post :create, role: params
98
+ expect(flash[:notice]).to eql "Perfil criado com sucesso"
99
+ end
100
+
101
+ it "should redirect to new role" do
102
+ post :create, role: params
103
+ expect(response).to redirect_to assigns(:role)
104
+ end
105
+
106
+ it "creates role" do
107
+ expect {
108
+ post :create, role: params
109
+ }.to change { Role.count }.by(1)
110
+
111
+ expect(assigns[:role].name).to eql "Minha Role"
112
+ expect(assigns[:role].features.first.name).to eq("Cadastrar")
113
+ end
114
+ end
115
+
116
+ context "when role is invalid" do
117
+ let(:params) { FactoryGirl.build(:role, name: '').attributes }
118
+
119
+ it "does not create user" do
120
+ expect {
121
+ post :create, role: params
122
+ }.to change { Role.count }.by(0)
123
+ end
124
+
125
+ it "should render new" do
126
+ post :create, role: params
127
+ expect(response).to render_template :new
128
+ end
129
+ end
130
+ end
131
+
132
+ describe "PUT update" do
133
+ let!(:role) { FactoryGirl.create(:role, name: 'Minha role') }
134
+
135
+ context "when user is valid" do
136
+ let(:params) { FactoryGirl.build(:role, name: 'Outra role').attributes }
137
+
138
+ it "sets flash message" do
139
+ put :update, id: role, role: params
140
+ expect(flash[:notice]).to eql "Perfil atualizado com sucesso"
141
+ end
142
+
143
+ it "redirects to user" do
144
+ put :update, id: role, role: params
145
+ expect(response).to redirect_to assigns(:role)
146
+ end
147
+
148
+ it "updates role" do
149
+ put :update, id: role, role: params
150
+ expect(assigns(:role).name).to eql "Outra role"
151
+ end
152
+ end
153
+
154
+ context "when role is invalid" do
155
+ let(:params) { FactoryGirl.build(:role, name: '').attributes }
156
+
157
+ it "does not create role" do
158
+ put :update, id: role, role: params
159
+ expect(assigns(:role).reload.name).to eql role.name
160
+ end
161
+
162
+ it "should render edit" do
163
+ put :update, id: role, role: params
164
+ expect(response).to render_template :edit
165
+ end
166
+ end
167
+ end
168
+ end
@@ -0,0 +1,112 @@
1
+ require "rails_helper"
2
+
3
+ describe Curupira::SessionsController do
4
+ describe "GET new" do
5
+ it "should get new" do
6
+ get :new
7
+ expect(response).to be_success
8
+ end
9
+
10
+ it "returns new user" do
11
+ get :new
12
+ expect(assigns(:user)).to be_new_record
13
+ end
14
+
15
+ context "when user is already logged in" do
16
+ it "redirects to root path" do
17
+ login_user(FactoryGirl.create :user)
18
+
19
+ get :new
20
+ expect(response).to redirect_to root_path
21
+ expect(flash[:alert]).to eql "Você já está logado"
22
+ end
23
+ end
24
+ end
25
+
26
+ describe "POST create" do
27
+ let(:user) { FactoryGirl.create :user, password: 123456 }
28
+
29
+ context "when credentials are valid" do
30
+ context "with email" do
31
+ it "create session" do
32
+ post :create, user: { username: user.email, password: 123456 }
33
+
34
+ expect(User.find(session[:user_id])).to eql user
35
+ end
36
+ end
37
+
38
+ context "with username" do
39
+ it "create session" do
40
+ post :create, user: { username: user.username, password: 123456 }
41
+
42
+ expect(User.find(session[:user_id])).to eql user
43
+ end
44
+ end
45
+
46
+ it "redirects to root path" do
47
+ post :create, user: { username: user.email, password: 123456 }
48
+
49
+ expect(response).to redirect_to root_path
50
+ end
51
+
52
+ it "renders flash notice" do
53
+ post :create, user: { username: user.email, password: 123456 }
54
+
55
+ expect(flash[:notice]).to eql "Bem vindo!"
56
+ end
57
+
58
+ context "when user is already logged in" do
59
+ it "redirects to root path" do
60
+ login_user(FactoryGirl.create :user)
61
+
62
+ post :create, user: { username: user.email, password: 123456 }
63
+ expect(response).to redirect_to root_path
64
+ expect(flash[:alert]).to eql "Você já está logado"
65
+ end
66
+ end
67
+ end
68
+
69
+ context "when credentials are invalid" do
70
+ it "renders flash alert" do
71
+ post :create, user: { username: user.email, password: "wrong pass" }
72
+
73
+ expect(flash[:alert]).to eql "Login ou senha inválidos"
74
+ end
75
+
76
+ it "renders new" do
77
+ post :create, user: { username: user.email, password: "wrong pass" }
78
+
79
+ expect(response).to render_template :new
80
+ end
81
+
82
+ it "returns new user" do
83
+ post :create, user: { username: user.email, password: "wrong pass" }
84
+
85
+ expect(assigns[:user]).to be_new_record
86
+ expect(assigns[:user].username).to eql user.email
87
+ expect(assigns[:user].password).to eql nil
88
+ end
89
+ end
90
+ end
91
+
92
+
93
+ describe "DELETE destroy" do
94
+ let(:user) { FactoryGirl.create :user }
95
+
96
+ it "deletes session" do
97
+ login_user(user)
98
+
99
+ expect(session[:user_id].to_i).to eql user.id
100
+ delete :destroy
101
+ expect(session[:user_id]).to eql nil
102
+ end
103
+
104
+ it "redirects to sign in path" do
105
+ login_user(user)
106
+
107
+ delete :destroy
108
+
109
+ expect(response).to redirect_to new_session_path
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,182 @@
1
+ require "rails_helper"
2
+
3
+ describe Curupira::UsersController do
4
+ let(:user) { FactoryGirl.create :user }
5
+
6
+ before do
7
+ login_user(user)
8
+ end
9
+
10
+ describe "GET index" do
11
+ let!(:group) { FactoryGirl.create(:group, users: [user, FactoryGirl.create(:user)]) }
12
+
13
+ it "should get index" do
14
+ get :index
15
+ expect(response).to be_success
16
+ end
17
+
18
+ it "returns all users" do
19
+ get :index
20
+ expect(assigns(:users).count).to eql 2
21
+ end
22
+ end
23
+
24
+ describe "GET show" do
25
+ context "when user exists" do
26
+ it "should get show" do
27
+ get :show, id: user
28
+ expect(response).to be_success
29
+ end
30
+
31
+ it "returns user" do
32
+ get :show, id: user
33
+ expect(assigns(:user)).to eql user
34
+ end
35
+ end
36
+
37
+ context "when user does not exist" do
38
+ it "renders 404" do
39
+ expect {
40
+ get :show, id: "wrong id"
41
+ }.to raise_error ActiveRecord::RecordNotFound
42
+ end
43
+ end
44
+ end
45
+
46
+ describe "GET new" do
47
+ it "should get new" do
48
+ get :new
49
+ expect(response).to be_success
50
+ end
51
+
52
+ it "returns new user" do
53
+ get :new
54
+ expect(assigns(:user)).to be_new_record
55
+ end
56
+ end
57
+
58
+ describe "GET edit" do
59
+ context "when user exists" do
60
+ it "should get edit" do
61
+ get :edit, id: user
62
+ expect(response).to be_success
63
+ end
64
+
65
+ it "returns user" do
66
+ get :edit, id: user
67
+ expect(assigns(:user)).to eql user
68
+ end
69
+ end
70
+
71
+ context "when user does not exist" do
72
+ it "renders 404" do
73
+ expect {
74
+ get :edit, id: "wrong id"
75
+ }.to raise_error ActiveRecord::RecordNotFound
76
+ end
77
+ end
78
+ end
79
+
80
+ describe "POST create" do
81
+ context "when user is valid" do
82
+ let(:group) { FactoryGirl.create :group, name: "Apple Corp" }
83
+ let(:role) { FactoryGirl.create :role, name: 'Minha Role' }
84
+ let(:params) { { email: "new_email@mail.com",
85
+ username: "new_username",
86
+ name: "New Name",
87
+ password: 12345678,
88
+ group_users_attributes: { "0": { group_id: group.id, role_group_users_attributes: { "0": { role_id: role.id } } } } } }
89
+
90
+ it "should redirect to new user" do
91
+ post :create, user: params
92
+ expect(flash[:notice]).to eql "Usuário criado com sucesso"
93
+ end
94
+
95
+ it "should redirect to new user" do
96
+ post :create, user: params
97
+ expect(response).to redirect_to assigns(:user)
98
+ end
99
+
100
+ it "creates user" do
101
+ expect {
102
+ post :create, user: params
103
+ }.to change { User.count }.by(1)
104
+
105
+ expect(assigns[:user].email).to eql "new_email@mail.com"
106
+ expect(assigns[:user].username).to eql "new_username"
107
+ expect(assigns[:user].name).to eql "New Name"
108
+ expect(assigns[:user].groups.first.name).to eql "Apple Corp"
109
+ expect(assigns[:user].role_group_users.first.role.name).to eql "Minha Role"
110
+ end
111
+ end
112
+
113
+ context "when user is invalid" do
114
+ let(:params) { { username: "new_username",
115
+ name: "New Name",
116
+ password: 12345678 } }
117
+
118
+ it "does not create user" do
119
+ expect {
120
+ post :create, user: params
121
+ }.to change { User.count }.by(0)
122
+ end
123
+
124
+ it "should render new" do
125
+ post :create, user: params
126
+ expect(response).to render_template :new
127
+ end
128
+ end
129
+ end
130
+
131
+ describe "PUT update" do
132
+ context "when user is valid" do
133
+ let(:group) { FactoryGirl.create :group }
134
+ let!(:group_user) { FactoryGirl.create :group_user, user: user, group: group }
135
+
136
+ let(:params) { { email: "new_email@mail.com",
137
+ username: "new_username",
138
+ name: "New Name",
139
+ password: 12345678,
140
+ group_users_attributes: { "0": { id: group.id, _destroy: true } } } }
141
+
142
+ it "sets flash message" do
143
+ put :update, id: user, user: params
144
+ expect(flash[:notice]).to eql "Usuário atualizado com sucesso"
145
+ end
146
+
147
+ it "redirects to user" do
148
+ put :update, id: user, user: params
149
+ expect(response).to redirect_to assigns(:user)
150
+ end
151
+
152
+ it "updates user" do
153
+ expect(user.groups).to_not be_empty
154
+
155
+ put :update, id: user, user: params
156
+ expect(assigns(:user).email).to eql "new_email@mail.com"
157
+ expect(assigns(:user).username).to eql "new_username"
158
+ expect(assigns(:user).name).to eql "New Name"
159
+ expect(user.groups).to be_empty
160
+ end
161
+ end
162
+
163
+ context "when user is invalid" do
164
+ let(:params) { { email: "",
165
+ username: "new_username",
166
+ name: "New Name",
167
+ password: 12345678 } }
168
+
169
+ it "does not create user" do
170
+ put :update, id: user, user: params
171
+ expect(assigns(:user).reload.email).to eql user.email
172
+ expect(assigns(:user).reload.username).to eql user.username
173
+ expect(assigns(:user).reload.name).to eql user.name
174
+ end
175
+
176
+ it "should render edit" do
177
+ put :update, id: user, user: params
178
+ expect(response).to render_template :edit
179
+ end
180
+ end
181
+ end
182
+ end