Skip to content

Commit

Permalink
Tests d'integration & verif cancan
Browse files Browse the repository at this point in the history
  • Loading branch information
kkaltrine committed Feb 26, 2016
1 parent ad06044 commit 2a03cd1
Show file tree
Hide file tree
Showing 17 changed files with 203 additions and 22 deletions.
1 change: 1 addition & 0 deletions app/controllers/galleries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def update
end
end

# Not used
# DELETE /galleries/1
# DELETE /galleries/1.json
def destroy
Expand Down
5 changes: 3 additions & 2 deletions app/controllers/pictures_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def new
end
end

# Not used
# GET /pictures/1/edit
def edit
#@gallery = Gallery.find(params[:gallery_id])
Expand Down Expand Up @@ -88,13 +89,13 @@ def update
# DELETE /pictures/1
# DELETE /pictures/1.json
def destroy
#@gallery = Gallery.find(params[:gallery_id])
#@picture = @gallery.pictures.find(params[:id])
@picture = Picture.find(params[:id])
@gallery = Gallery.find(@picture.gallery_id)
@picture.destroy

respond_to do |format|
format.html { redirect_to root_path , notice: 'Picture was successfully deleted.'}
format.html { redirect_to gallery_path(@gallery) , notice: 'Picture was successfully deleted.'}
format.js
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def initialize(user)
can :create, Gallery
can :read, Gallery, :user_id => user.id
can :update, Gallery, :user_id => user.id
cannot :destroy, Gallery, :user_id => user.id
cannot :destroy, Gallery
can :create, Picture
can :read, Picture, :gallery => {:user_id => user.id}
cannot :update, Picture
Expand Down
3 changes: 3 additions & 0 deletions app/views/galleries/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<%= image_tag picture.image.url(:thumb) %>
<% end %>
<% end %>
<% if can? :update, @gallery %>

<%= form_for @gallery, :html => {:class => 'form-horizontal', multipart: true} do |f| %>

<div class="control-group">
Expand All @@ -27,3 +29,4 @@
galleries_path, :class => 'btn btn-mini' %>
</div>
<% end %>
<% end %>
7 changes: 5 additions & 2 deletions app/views/galleries/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<div class="row">
<% if can? :read, @gallery %>

<div class="row">
<% if @galleries.blank? %>
<h1>Galleries</h1>
<%= link_to 'New', new_gallery_path, :class => 'btn btn-mini' %>
Expand Down Expand Up @@ -30,4 +32,5 @@
<% end %>
</ul>

</div>
</div>
<% end %>
1 change: 1 addition & 0 deletions test/controllers/users_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'test_helper'

class UsersControllerTest < ActionController::TestCase
include Devise::TestHelpers
# test "the truth" do
# assert true
# end
Expand Down
12 changes: 7 additions & 5 deletions test/fixtures/galleries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
one:
id: 1
user_id: 1

two:
id: 2
user_id: 2
2 changes: 1 addition & 1 deletion test/fixtures/levels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ twentynine:
fr: C1 Avancé
ch: C1 Avancé

twentynine:
thirty:
level: 30
code: Level::LEVEL_CODE[2]
be: C2 Compétent/Courant
Expand Down
18 changes: 13 additions & 5 deletions test/fixtures/pictures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
one:
id: 1
gallery_id: 1
image_file_name: "ff139873-aa07-4854-8697-7097efd096f7-original.jpeg"
image_content_type: "image/jpeg"
image_file_size: 120665

two:
id: 2
gallery_id: 2
image_file_name: "ff139873-aa07-4854-8697-7097efd096f7-original.jpeg"
image_content_type: "image/jpeg"
image_file_size: 120665
6 changes: 3 additions & 3 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ one:
failed_attempts: default
admin: false
type: User
confirmed_at: "2016-02-25 16:15:52"
# column: value
#
two:
id: 2
Expand All @@ -49,4 +47,6 @@ two:
failed_attempts: default
admin: false
type: User
confirmed_at: "2016-02-25 16:15:52"
confirmation_sent_at: "2016-02-25 16:15:52"
confirmed_at: "2016-02-25 16:15:52"
confirmation_token: ace8cb0764a0e56e75cc0196dd2df6fa0b5005f7d31646c694f880d00b8206b
29 changes: 29 additions & 0 deletions test/integration/gallery_edit_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'test_helper'

class GalleryEditTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end

test "get gallery" do
post user_session_path, 'user[email]' => 'y@y.y', 'user[password]' => 'kaltrina'
@gallery=Gallery.where(:user_id => User.find(2)).last.id
get gallery_path(@gallery)
assert_equal 200, status
end
test "get edit gallery" do
post user_session_path, 'user[email]' => 'y@y.y', 'user[password]' => 'kaltrina'
@gallery=Gallery.where(:user_id => User.find(2)).last.id
get edit_gallery_path(@gallery)
assert_equal 200, status
end
test "delete gallery" do
post user_session_path, 'user[email]' => 'y@y.y', 'user[password]' => 'kaltrina'
assert_no_difference 'Gallery.count' do
@gallery = Gallery.where(:user_id => User.find(2)).last
delete gallery_path(@gallery)
end
assert_redirected_to root_path
assert_equal 302, status
end
end
21 changes: 21 additions & 0 deletions test/integration/picture_new_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'test_helper'

class PictureNewTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end

test "get picture" do
post user_session_path, 'user[email]' => 'y@y.y', 'user[password]' => 'kaltrina'
@picture=Gallery.where(:user_id=>User.find(2)).last.pictures.last
get picture_path(@picture)
assert_equal 200, status
end
test "delete picture" do
post user_session_path, 'user[email]' => 'y@y.y', 'user[password]' => 'kaltrina'
@gallery = Gallery.where(:user_id=>User.find(2)).last
@picture=@gallery.pictures.last
delete picture_path(@picture)
assert_redirected_to gallery_path(@gallery)
end
end
27 changes: 27 additions & 0 deletions test/integration/user_resendinstructions_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'test_helper'

class UserResendinstructionsTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
test "valid resend instructions" do
get new_user_confirmation_path
post user_confirmation_path, 'user[email]' => 'k@k.k'
assert_redirected_to new_user_session_path
end
test "invalid resend instructions : wrong email" do
get new_user_confirmation_path
post user_confirmation_path, 'user[email]' => 'y@k.k'
assert_template 'devise/confirmations/new'
end
test "invalid resend instructions : empty email" do
get new_user_confirmation_path
post user_confirmation_path, 'user[email]' => ''
assert_template 'devise/confirmations/new'
end
test "invalid resend instructions : already confirmed" do
get new_user_confirmation_path
post user_confirmation_path, 'user[email]' => 'y@y.y'
assert_template 'devise/confirmations/new'
end
end
23 changes: 23 additions & 0 deletions test/integration/user_resetpassword_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'test_helper'

class UserResetpasswordTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end

test "valid reset pwd" do
get new_user_password_path
post user_password_path, 'user[email]' => 'y@y.y'
assert_redirected_to new_user_session_path
end
test "invalid reset pwd : wrong email" do
get new_user_password_path
post user_password_path, 'user[email]' => 'k@y.y'
assert_template 'devise/passwords/new'
end
test "invalid reset pwd : empty email" do
get new_user_password_path
post user_password_path, 'user[email]' => ''
assert_template 'devise/passwords/new'
end
end
28 changes: 28 additions & 0 deletions test/integration/user_unlockinstructions_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'test_helper'

class UserUnlockinstructionsTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
test "valid unlock instructions" do
User.find(1).lock_access!
get new_user_unlock_path
post user_unlock_path, 'user[email]' => 'k@k.k'
assert_redirected_to new_user_session_path
end
test "invalid unlock instructions : wrong email" do
get new_user_unlock_path
post user_unlock_path, 'user[email]' => 'y@k.k'
assert_template 'devise/unlocks/new'
end
test "invalid unlock instructions : empty email" do
get new_user_unlock_path
post user_unlock_path, 'user[email]' => ''
assert_template 'devise/unlocks/new'
end
test "invalid unlock instructions : not locked" do
get new_user_unlock_path
post user_unlock_path, 'user[email]' => 'y@y.y'
assert_template 'devise/unlocks/new'
end
end
38 changes: 36 additions & 2 deletions test/integration/users_signin_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,41 @@ class UsersSigninTest < ActionDispatch::IntegrationTest
test "valid signin information" do
get new_user_session_path
post user_session_path, 'user[email]' => 'y@y.y', 'user[password]' => 'kaltrina'


assert_redirected_to root_path
end
test "invalid signin information: wrong pwd" do
get new_user_session_path
post user_session_path, 'user[email]' => 'y@y.y', 'user[password]' => 'kaltrinaa'
assert_template 'devise/sessions/new'
end
test "invalid signin information : no user with email" do
get new_user_session_path
post user_session_path, 'user[email]' => 'k@y.y', 'user[password]' => 'kaltrina'
assert_template 'devise/sessions/new'
end
test "invalid signin information : no information" do
get new_user_session_path
post user_session_path, 'user[email]' => '', 'user[password]' => ''
assert_template 'devise/sessions/new'
end
test "invalid signin information : no user" do
get new_user_session_path
post user_session_path, 'user[email]' => '', 'user[password]' => 'kaltrina'
assert_template 'devise/sessions/new'
end
test "invalid signin information : no pwd" do
get new_user_session_path
post user_session_path, 'user[email]' => 'y@y.y', 'user[password]' => ''
assert_template 'devise/sessions/new'
end
test "post sur route definie en get" do
post new_user_session_path
assert_template 'devise/sessions/new'
end
test "get sur route definie en post" do
get new_user_session_path
get user_session_path
assert_template 'devise/sessions/new'
end

end
2 changes: 1 addition & 1 deletion test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class UserTest < ActiveSupport::TestCase
test "users_count" do
assert_equal 1, User.count
assert_equal 2, User.count
end

test "user_type" do
Expand Down

0 comments on commit 2a03cd1

Please sign in to comment.