Skip to content

Commit 9ef6236

Browse files
dadachiclaude
andauthored
Add tests for previously uncovered code paths (#47)
Covers ErrorsController, AdminUser, the static ShopkeeperAuth::{ConfirmationResults,ResetPasswords} pages, ShopkeeperMailer, and Shopkeeper::NotificationMailer (invited, confirmation_instructions, reset_password_instructions). Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 055d762 commit 9ef6236

6 files changed

Lines changed: 214 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require "test_helper"
2+
3+
class ErrorsControllerTest < ActionController::TestCase
4+
tests ErrorsController
5+
6+
test "not_found responds with 404 JSON when content-type is JSON" do
7+
@request.headers["Content-Type"] = "application/json"
8+
get :not_found
9+
10+
assert_response :not_found
11+
body = JSON.parse(response.body)
12+
assert_equal 404, body["code"]
13+
assert_equal "Not found.", body["error_message"]
14+
end
15+
16+
test "not_found responds with 404 HTML when content-type is not JSON" do
17+
get :not_found
18+
19+
assert_response :not_found
20+
assert_match(/text\/html/, response.media_type)
21+
end
22+
23+
test "internal_server_error responds with 500 JSON when content-type is JSON" do
24+
@request.headers["Content-Type"] = "application/json"
25+
get :internal_server_error
26+
27+
assert_response :internal_server_error
28+
body = JSON.parse(response.body)
29+
assert_equal 500, body["code"]
30+
assert_equal "Internal server error.", body["error_message"]
31+
end
32+
33+
test "internal_server_error responds with 500 HTML when content-type is not JSON" do
34+
get :internal_server_error
35+
36+
assert_response :internal_server_error
37+
assert_match(/text\/html/, response.media_type)
38+
end
39+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require "test_helper"
2+
3+
class ShopkeeperAuth::ConfirmationResultsControllerTest < ActionDispatch::IntegrationTest
4+
test "show renders the success page" do
5+
get shopkeeper_auth_confirmation_result_url
6+
7+
assert_response :success
8+
assert_match I18n.t("devise_token_auth.confirmations.successfully_confirmed"), response.body
9+
end
10+
11+
test "show uses the minimal layout" do
12+
get shopkeeper_auth_confirmation_result_url
13+
14+
assert_response :success
15+
assert_match(/text\/html/, response.media_type)
16+
end
17+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require "test_helper"
2+
3+
class ShopkeeperAuth::ResetPasswordsControllerTest < ActionDispatch::IntegrationTest
4+
test "show renders the success page" do
5+
get shopkeeper_auth_reset_password_url
6+
7+
assert_response :success
8+
assert_match I18n.t("devise_token_auth.passwords.successfully_updated"), response.body
9+
end
10+
11+
test "edit renders the password change form with minimum password length" do
12+
get edit_shopkeeper_auth_reset_password_url(reset_password_token: "abc123")
13+
14+
assert_response :success
15+
assert_match I18n.t("change_your_password"), response.body
16+
assert_match ConfigSettings.minimum_password_length.to_s, response.body
17+
end
18+
19+
test "edit includes the reset_password_token in the form" do
20+
get edit_shopkeeper_auth_reset_password_url(reset_password_token: "tok-xyz")
21+
22+
assert_response :success
23+
assert_match "tok-xyz", response.body
24+
end
25+
end
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
require "test_helper"
2+
3+
class Shopkeeper::NotificationMailerTest < ActionMailer::TestCase
4+
setup do
5+
@shopkeeper = shopkeepers(:one)
6+
@shopkeeper.create_default_account
7+
@account = @shopkeeper.accounts.first
8+
@invitation = AccountsInvitation.create!(
9+
account: @account,
10+
name: "Invited User",
11+
email: "invited@example.com",
12+
member: true,
13+
invited_by: @shopkeeper
14+
)
15+
end
16+
17+
test "invited renders the expected subject, recipient, and body" do
18+
mail = Shopkeeper::NotificationMailer.with(accounts_invitation: @invitation).invited
19+
20+
expected_subject = I18n.t(
21+
"shopkeeper.notification_mailer.invited.subject",
22+
inviter: @shopkeeper.name,
23+
account: @account.name
24+
)
25+
assert_equal expected_subject, mail.subject
26+
assert_equal [@invitation.email], mail.to
27+
assert_equal [ConfigSettings.email.default_from], mail.from
28+
assert_match @invitation.token, mail.body.encoded
29+
assert_match @account.name, mail.body.encoded
30+
end
31+
32+
test "confirmation_instructions renders the expected subject, recipient, and body" do
33+
mail = Shopkeeper::NotificationMailer.with(
34+
resource: @shopkeeper,
35+
token: "confirm-token-123",
36+
opts: {redirect_url: "http://example.com/confirm", client_config: "default"}
37+
).confirmation_instructions
38+
39+
assert_equal I18n.t("shopkeeper.notification_mailer.confirmation_instructions.subject"), mail.subject
40+
assert_equal [@shopkeeper.email], mail.to
41+
assert_match "confirm-token-123", mail.body.encoded
42+
assert_match @shopkeeper.email, mail.body.encoded
43+
end
44+
45+
test "confirmation_instructions uses opts[:to] when provided" do
46+
mail = Shopkeeper::NotificationMailer.with(
47+
resource: @shopkeeper,
48+
token: "tok",
49+
opts: {to: "override@example.com", redirect_url: "http://example.com/confirm", client_config: "default"}
50+
).confirmation_instructions
51+
52+
assert_equal ["override@example.com"], mail.to
53+
assert_match "override@example.com", mail.body.encoded
54+
end
55+
56+
test "reset_password_instructions renders the expected subject, recipient, and body" do
57+
mail = Shopkeeper::NotificationMailer.with(
58+
resource: @shopkeeper,
59+
token: "reset-token-xyz",
60+
opts: {redirect_url: "http://example.com/reset", client_config: "default"}
61+
).reset_password_instructions
62+
63+
assert_equal I18n.t("shopkeeper.notification_mailer.reset_password_instructions.subject"), mail.subject
64+
assert_equal [@shopkeeper.email], mail.to
65+
assert_match "reset-token-xyz", mail.body.encoded
66+
end
67+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require "test_helper"
2+
3+
class ShopkeeperMailerTest < ActionMailer::TestCase
4+
test "inherits from ApplicationMailer" do
5+
assert_operator ShopkeeperMailer, :<, ApplicationMailer
6+
end
7+
8+
test "uses the application default_from address" do
9+
assert_equal ConfigSettings.email.default_from_with_name, ShopkeeperMailer.default[:from]
10+
end
11+
12+
test "uses the mailer layout" do
13+
assert_equal "mailer", ShopkeeperMailer._layout
14+
end
15+
end

test/models/admin_user_test.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
require "test_helper"
2+
3+
class AdminUserTest < ActiveSupport::TestCase
4+
def valid_attributes
5+
{
6+
name: "Admin",
7+
email: "admin@example.com",
8+
password: "password"
9+
}
10+
end
11+
12+
test "should be valid with valid attributes" do
13+
admin_user = AdminUser.new(valid_attributes)
14+
assert admin_user.valid?
15+
end
16+
17+
test "should require email" do
18+
admin_user = AdminUser.new(valid_attributes.merge(email: nil))
19+
assert_not admin_user.valid?
20+
assert_includes admin_user.errors[:email], "can't be blank"
21+
end
22+
23+
test "should require unique email" do
24+
AdminUser.create!(valid_attributes)
25+
duplicate = AdminUser.new(valid_attributes)
26+
assert_not duplicate.valid?
27+
assert_includes duplicate.errors[:email], "has already been taken"
28+
end
29+
30+
test "should reject invalid email format" do
31+
admin_user = AdminUser.new(valid_attributes.merge(email: "not-an-email"))
32+
assert_not admin_user.valid?
33+
assert_not_empty admin_user.errors[:email]
34+
end
35+
36+
test "should hash password using has_secure_password" do
37+
admin_user = AdminUser.create!(valid_attributes)
38+
assert_not_nil admin_user.password_digest
39+
assert_not_equal "password", admin_user.password_digest
40+
end
41+
42+
test "authenticate returns admin_user when password matches" do
43+
admin_user = AdminUser.create!(valid_attributes)
44+
assert_equal admin_user, admin_user.authenticate("password")
45+
end
46+
47+
test "authenticate returns false when password does not match" do
48+
admin_user = AdminUser.create!(valid_attributes)
49+
assert_equal false, admin_user.authenticate("wrong-password")
50+
end
51+
end

0 commit comments

Comments
 (0)