-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from felipejoglar/felipejoglar/add-tests
Add tests
- Loading branch information
Showing
15 changed files
with
407 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# Copyright 2024 Felipe Joglar | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
require "test_helper" | ||
|
||
class PasswordResetControllerTest < ActionDispatch::IntegrationTest | ||
setup do | ||
@name = "Felipe" | ||
@token = create_user_with(name: @name).generate_token_for(:password_reset) | ||
end | ||
|
||
test "requesting to create a new password" do | ||
get forgot_password_url | ||
|
||
assert_response :ok | ||
assert_select 'h2', I18n .t("auth.forgot_password.title") | ||
|
||
post password_url, params: password_reset_params(email: "felipe@taskodoro.com") | ||
follow_redirect! | ||
|
||
assert_response :ok | ||
assert_select 'div.notice', I18n.t("auth.password_reset.message.confirmation") | ||
end | ||
|
||
test "updating a password" do | ||
get password_edit_url, params: { token: @token } | ||
|
||
assert_response :ok | ||
assert_select 'h2', I18n.t("auth.password_reset.title") | ||
|
||
patch "#{password_url}?token=#{@token}", params: new_password_params | ||
assert_response :found | ||
|
||
follow_redirect! | ||
assert_response :ok | ||
assert_select 'h1', "Welcome #{@name}" | ||
end | ||
|
||
test "failing to update a password with invalid token" do | ||
get password_edit_url, params: { token: @token } | ||
|
||
assert_response :ok | ||
assert_select 'h2', I18n.t("auth.password_reset.title") | ||
|
||
patch "#{password_url}?token=not_valid_token", params: new_password_params | ||
|
||
follow_redirect! | ||
assert_response :ok | ||
assert_equal I18n.t("auth.password_reset.message.expired"), flash[:notice] | ||
end | ||
|
||
test "failing to update a password with invalid password" do | ||
get password_edit_url, params: { token: @token } | ||
|
||
assert_response :ok | ||
assert_select 'h2', I18n.t("auth.password_reset.title") | ||
|
||
patch "#{password_url}?token=#{@token}", params: new_password_params(password: "invalid") | ||
assert_response :unprocessable_entity | ||
|
||
assert_select 'h2', I18n.t("auth.password_reset.title") | ||
end | ||
|
||
private | ||
|
||
def create_user_with(name:) | ||
User.create!( | ||
name: name, | ||
email: "felipe@taskodoro.com", | ||
password: "a_valid_password", | ||
password_confirmation: "a_valid_password" | ||
) | ||
end | ||
|
||
def password_reset_params(email:) | ||
{ user: { email: email } } | ||
end | ||
|
||
def new_password_params(password: "a_valid_password") | ||
{ user: | ||
{ | ||
password: password, | ||
password_confirmation: password | ||
} | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,91 @@ | ||
# Copyright 2024 Felipe Joglar | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
require "test_helper" | ||
|
||
class SessionsControllerTest < ActionDispatch::IntegrationTest | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
|
||
test "creating a new session" do | ||
email = "felipe@taskodoro.com" | ||
password = "a_valid_password" | ||
|
||
get signin_url | ||
|
||
assert_response :ok | ||
assert_select 'h2', I18n.t("auth.sign_in.title") | ||
|
||
create_user_with(email: email, password: password) | ||
|
||
post signin_url, params: login_params(email: email, password: password) | ||
assert session["current_user_id"], "User should be logged in after creation" | ||
|
||
follow_redirect! | ||
assert_response :ok | ||
assert_select 'h1', 'Welcome Felipe' | ||
end | ||
|
||
test "failing to create a new session" do | ||
email = "felipe@taskodoro.com" | ||
password = "a_valid_password" | ||
|
||
get signin_url | ||
|
||
assert_response :ok | ||
assert_select 'h2', I18n.t("auth.sign_in.title") | ||
|
||
create_user_with(email: email, password: password) | ||
|
||
post signin_url, params: login_params(email: "not_signed_up@email.com", password: password) | ||
assert_response :unprocessable_entity | ||
|
||
assert_select 'div.alert', 1 | ||
end | ||
|
||
test "destroy current session" do | ||
email = "felipe@taskodoro.com" | ||
password = "a_valid_password" | ||
|
||
create_user_with(email: email, password: password) | ||
post signin_url, params: login_params(email: email, password: password) | ||
assert session["current_user_id"], "User should be logged in after creation" | ||
follow_redirect! | ||
|
||
delete signout_url | ||
refute session["current_user_id"] | ||
|
||
follow_redirect! | ||
assert_select 'h1', I18n.t("landing.welcome") | ||
end | ||
|
||
private | ||
|
||
def create_user_with(email:, password:) | ||
User.create!( | ||
name: "Felipe", | ||
email: email, | ||
password: password, | ||
password_confirmation: password | ||
) | ||
end | ||
|
||
def login_params(email:, password:) | ||
{ | ||
user: | ||
{ | ||
email: email, | ||
password: password | ||
} | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,69 @@ | ||
# Copyright 2024 Felipe Joglar | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
require "test_helper" | ||
|
||
class UsersControllerTest < ActionDispatch::IntegrationTest | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
|
||
test "creating a new user" do | ||
get signup_url | ||
|
||
assert_response :ok | ||
assert_select 'h2', I18n.t("auth.sign_up.title") | ||
|
||
post signup_url, params: valid_params(name: "Felipe") | ||
assert session["current_user_id"], "User should be logged in after creation" | ||
|
||
follow_redirect! | ||
assert_response :ok | ||
assert_select 'h1', 'Welcome Felipe' | ||
end | ||
|
||
test "failing to create a new user" do | ||
get signup_url | ||
|
||
assert_response :ok | ||
assert_select 'h2', I18n.t("auth.sign_up.title") | ||
|
||
post signup_url, params: invalid_params | ||
assert_response :unprocessable_entity | ||
|
||
assert_select 'div.alert', 1 | ||
end | ||
|
||
private | ||
|
||
def valid_params(name:, password: "a_valid_password") | ||
{ user: | ||
{ | ||
name: name, | ||
email: "avalid@email", | ||
password: password, | ||
password_confirmation: password | ||
} | ||
} | ||
end | ||
|
||
def invalid_params | ||
{ user: | ||
{ | ||
name: "A name", | ||
email: "avalid@email", | ||
password: "a_valid_password", | ||
password_confirmation: "no_matching_password" | ||
} | ||
} | ||
end | ||
end | ||
|
Oops, something went wrong.