mirror of
https://github.com/we-promise/sure.git
synced 2026-04-19 03:54:08 +00:00
chore: normalize line endings in tags_controller_test.rb (#848)
Convert CRLF to LF line endings for consistency. https://claude.ai/code/session_012VFw2enx8PaDGqoHYqZ6jP Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,262 +1,262 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require "test_helper"
|
require "test_helper"
|
||||||
|
|
||||||
class Api::V1::TagsControllerTest < ActionDispatch::IntegrationTest
|
class Api::V1::TagsControllerTest < ActionDispatch::IntegrationTest
|
||||||
setup do
|
setup do
|
||||||
@user = users(:family_admin)
|
@user = users(:family_admin)
|
||||||
@other_family_user = users(:empty)
|
@other_family_user = users(:empty)
|
||||||
|
|
||||||
@oauth_app = Doorkeeper::Application.create!(
|
@oauth_app = Doorkeeper::Application.create!(
|
||||||
name: "Test App",
|
name: "Test App",
|
||||||
redirect_uri: "https://example.com/callback",
|
redirect_uri: "https://example.com/callback",
|
||||||
scopes: "read read_write"
|
scopes: "read read_write"
|
||||||
)
|
)
|
||||||
|
|
||||||
@read_token = Doorkeeper::AccessToken.create!(
|
@read_token = Doorkeeper::AccessToken.create!(
|
||||||
application: @oauth_app,
|
application: @oauth_app,
|
||||||
resource_owner_id: @user.id,
|
resource_owner_id: @user.id,
|
||||||
scopes: "read"
|
scopes: "read"
|
||||||
)
|
)
|
||||||
|
|
||||||
@read_write_token = Doorkeeper::AccessToken.create!(
|
@read_write_token = Doorkeeper::AccessToken.create!(
|
||||||
application: @oauth_app,
|
application: @oauth_app,
|
||||||
resource_owner_id: @user.id,
|
resource_owner_id: @user.id,
|
||||||
scopes: "read_write"
|
scopes: "read_write"
|
||||||
)
|
)
|
||||||
|
|
||||||
@tag = @user.family.tags.create!(name: "Test Tag #{SecureRandom.hex(4)}", color: "#3b82f6")
|
@tag = @user.family.tags.create!(name: "Test Tag #{SecureRandom.hex(4)}", color: "#3b82f6")
|
||||||
end
|
end
|
||||||
|
|
||||||
# Index action tests
|
# Index action tests
|
||||||
test "index requires authentication" do
|
test "index requires authentication" do
|
||||||
get api_v1_tags_url
|
get api_v1_tags_url
|
||||||
|
|
||||||
assert_response :unauthorized
|
assert_response :unauthorized
|
||||||
end
|
end
|
||||||
|
|
||||||
test "index returns user's family tags successfully" do
|
test "index returns user's family tags successfully" do
|
||||||
get api_v1_tags_url, headers: read_headers
|
get api_v1_tags_url, headers: read_headers
|
||||||
|
|
||||||
assert_response :success
|
assert_response :success
|
||||||
|
|
||||||
tags = JSON.parse(response.body)
|
tags = JSON.parse(response.body)
|
||||||
assert_kind_of Array, tags
|
assert_kind_of Array, tags
|
||||||
assert tags.length >= 1
|
assert tags.length >= 1
|
||||||
|
|
||||||
tag = tags.first
|
tag = tags.first
|
||||||
assert tag.key?("id")
|
assert tag.key?("id")
|
||||||
assert tag.key?("name")
|
assert tag.key?("name")
|
||||||
assert tag.key?("color")
|
assert tag.key?("color")
|
||||||
assert tag.key?("created_at")
|
assert tag.key?("created_at")
|
||||||
assert tag.key?("updated_at")
|
assert tag.key?("updated_at")
|
||||||
end
|
end
|
||||||
|
|
||||||
test "index does not return tags from other families" do
|
test "index does not return tags from other families" do
|
||||||
other_tag = @other_family_user.family.tags.create!(name: "Other Tag", color: "#3b82f6")
|
other_tag = @other_family_user.family.tags.create!(name: "Other Tag", color: "#3b82f6")
|
||||||
|
|
||||||
get api_v1_tags_url, headers: read_headers
|
get api_v1_tags_url, headers: read_headers
|
||||||
|
|
||||||
assert_response :success
|
assert_response :success
|
||||||
tags = JSON.parse(response.body)
|
tags = JSON.parse(response.body)
|
||||||
tag_ids = tags.map { |t| t["id"] }
|
tag_ids = tags.map { |t| t["id"] }
|
||||||
|
|
||||||
assert_includes tag_ids, @tag.id
|
assert_includes tag_ids, @tag.id
|
||||||
assert_not_includes tag_ids, other_tag.id
|
assert_not_includes tag_ids, other_tag.id
|
||||||
end
|
end
|
||||||
|
|
||||||
# Show action tests
|
# Show action tests
|
||||||
test "show requires authentication" do
|
test "show requires authentication" do
|
||||||
get api_v1_tag_url(@tag)
|
get api_v1_tag_url(@tag)
|
||||||
|
|
||||||
assert_response :unauthorized
|
assert_response :unauthorized
|
||||||
end
|
end
|
||||||
|
|
||||||
test "show returns tag successfully" do
|
test "show returns tag successfully" do
|
||||||
get api_v1_tag_url(@tag), headers: read_headers
|
get api_v1_tag_url(@tag), headers: read_headers
|
||||||
|
|
||||||
assert_response :success
|
assert_response :success
|
||||||
|
|
||||||
tag = JSON.parse(response.body)
|
tag = JSON.parse(response.body)
|
||||||
assert_equal @tag.id, tag["id"]
|
assert_equal @tag.id, tag["id"]
|
||||||
assert_equal @tag.name, tag["name"]
|
assert_equal @tag.name, tag["name"]
|
||||||
assert_equal "#3b82f6", tag["color"]
|
assert_equal "#3b82f6", tag["color"]
|
||||||
end
|
end
|
||||||
|
|
||||||
test "show returns 404 for non-existent tag" do
|
test "show returns 404 for non-existent tag" do
|
||||||
get api_v1_tag_url(id: SecureRandom.uuid), headers: read_headers
|
get api_v1_tag_url(id: SecureRandom.uuid), headers: read_headers
|
||||||
|
|
||||||
assert_response :not_found
|
assert_response :not_found
|
||||||
end
|
end
|
||||||
|
|
||||||
test "show returns 404 for tag from another family" do
|
test "show returns 404 for tag from another family" do
|
||||||
other_tag = @other_family_user.family.tags.create!(name: "Other Tag", color: "#3b82f6")
|
other_tag = @other_family_user.family.tags.create!(name: "Other Tag", color: "#3b82f6")
|
||||||
|
|
||||||
get api_v1_tag_url(other_tag), headers: read_headers
|
get api_v1_tag_url(other_tag), headers: read_headers
|
||||||
|
|
||||||
assert_response :not_found
|
assert_response :not_found
|
||||||
end
|
end
|
||||||
|
|
||||||
# Create action tests
|
# Create action tests
|
||||||
test "create requires authentication" do
|
test "create requires authentication" do
|
||||||
post api_v1_tags_url, params: { tag: { name: "New Tag" } }
|
post api_v1_tags_url, params: { tag: { name: "New Tag" } }
|
||||||
|
|
||||||
assert_response :unauthorized
|
assert_response :unauthorized
|
||||||
end
|
end
|
||||||
|
|
||||||
test "create requires read_write scope" do
|
test "create requires read_write scope" do
|
||||||
post api_v1_tags_url,
|
post api_v1_tags_url,
|
||||||
params: { tag: { name: "New Tag", color: "#4da568" } },
|
params: { tag: { name: "New Tag", color: "#4da568" } },
|
||||||
headers: read_headers
|
headers: read_headers
|
||||||
|
|
||||||
assert_response :forbidden
|
assert_response :forbidden
|
||||||
end
|
end
|
||||||
|
|
||||||
test "create tag successfully" do
|
test "create tag successfully" do
|
||||||
tag_name = "New Tag #{SecureRandom.hex(4)}"
|
tag_name = "New Tag #{SecureRandom.hex(4)}"
|
||||||
|
|
||||||
assert_difference -> { @user.family.tags.count }, 1 do
|
assert_difference -> { @user.family.tags.count }, 1 do
|
||||||
post api_v1_tags_url,
|
post api_v1_tags_url,
|
||||||
params: { tag: { name: tag_name, color: "#4da568" } },
|
params: { tag: { name: tag_name, color: "#4da568" } },
|
||||||
headers: read_write_headers
|
headers: read_write_headers
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_response :created
|
assert_response :created
|
||||||
|
|
||||||
tag = JSON.parse(response.body)
|
tag = JSON.parse(response.body)
|
||||||
assert_equal tag_name, tag["name"]
|
assert_equal tag_name, tag["name"]
|
||||||
assert_equal "#4da568", tag["color"]
|
assert_equal "#4da568", tag["color"]
|
||||||
end
|
end
|
||||||
|
|
||||||
test "create tag with auto-assigned color" do
|
test "create tag with auto-assigned color" do
|
||||||
tag_name = "Auto Color Tag #{SecureRandom.hex(4)}"
|
tag_name = "Auto Color Tag #{SecureRandom.hex(4)}"
|
||||||
|
|
||||||
post api_v1_tags_url,
|
post api_v1_tags_url,
|
||||||
params: { tag: { name: tag_name } },
|
params: { tag: { name: tag_name } },
|
||||||
headers: read_write_headers
|
headers: read_write_headers
|
||||||
|
|
||||||
assert_response :created
|
assert_response :created
|
||||||
|
|
||||||
tag = JSON.parse(response.body)
|
tag = JSON.parse(response.body)
|
||||||
assert_equal tag_name, tag["name"]
|
assert_equal tag_name, tag["name"]
|
||||||
assert tag["color"].present?
|
assert tag["color"].present?
|
||||||
end
|
end
|
||||||
|
|
||||||
test "create fails with duplicate name" do
|
test "create fails with duplicate name" do
|
||||||
post api_v1_tags_url,
|
post api_v1_tags_url,
|
||||||
params: { tag: { name: @tag.name } },
|
params: { tag: { name: @tag.name } },
|
||||||
headers: read_write_headers
|
headers: read_write_headers
|
||||||
|
|
||||||
assert_response :unprocessable_entity
|
assert_response :unprocessable_entity
|
||||||
end
|
end
|
||||||
|
|
||||||
# Update action tests
|
# Update action tests
|
||||||
test "update requires authentication" do
|
test "update requires authentication" do
|
||||||
patch api_v1_tag_url(@tag), params: { tag: { name: "Updated" } }
|
patch api_v1_tag_url(@tag), params: { tag: { name: "Updated" } }
|
||||||
|
|
||||||
assert_response :unauthorized
|
assert_response :unauthorized
|
||||||
end
|
end
|
||||||
|
|
||||||
test "update requires read_write scope" do
|
test "update requires read_write scope" do
|
||||||
patch api_v1_tag_url(@tag),
|
patch api_v1_tag_url(@tag),
|
||||||
params: { tag: { name: "Updated" } },
|
params: { tag: { name: "Updated" } },
|
||||||
headers: read_headers
|
headers: read_headers
|
||||||
|
|
||||||
assert_response :forbidden
|
assert_response :forbidden
|
||||||
end
|
end
|
||||||
|
|
||||||
test "update tag successfully" do
|
test "update tag successfully" do
|
||||||
new_name = "Updated Tag #{SecureRandom.hex(4)}"
|
new_name = "Updated Tag #{SecureRandom.hex(4)}"
|
||||||
|
|
||||||
patch api_v1_tag_url(@tag),
|
patch api_v1_tag_url(@tag),
|
||||||
params: { tag: { name: new_name, color: "#db5a54" } },
|
params: { tag: { name: new_name, color: "#db5a54" } },
|
||||||
headers: read_write_headers
|
headers: read_write_headers
|
||||||
|
|
||||||
assert_response :success
|
assert_response :success
|
||||||
|
|
||||||
tag = JSON.parse(response.body)
|
tag = JSON.parse(response.body)
|
||||||
assert_equal new_name, tag["name"]
|
assert_equal new_name, tag["name"]
|
||||||
assert_equal "#db5a54", tag["color"]
|
assert_equal "#db5a54", tag["color"]
|
||||||
end
|
end
|
||||||
|
|
||||||
test "update tag partially" do
|
test "update tag partially" do
|
||||||
original_name = @tag.name
|
original_name = @tag.name
|
||||||
|
|
||||||
patch api_v1_tag_url(@tag),
|
patch api_v1_tag_url(@tag),
|
||||||
params: { tag: { color: "#eb5429" } },
|
params: { tag: { color: "#eb5429" } },
|
||||||
headers: read_write_headers
|
headers: read_write_headers
|
||||||
|
|
||||||
assert_response :success
|
assert_response :success
|
||||||
|
|
||||||
tag = JSON.parse(response.body)
|
tag = JSON.parse(response.body)
|
||||||
assert_equal original_name, tag["name"]
|
assert_equal original_name, tag["name"]
|
||||||
assert_equal "#eb5429", tag["color"]
|
assert_equal "#eb5429", tag["color"]
|
||||||
end
|
end
|
||||||
|
|
||||||
test "update returns 404 for non-existent tag" do
|
test "update returns 404 for non-existent tag" do
|
||||||
patch api_v1_tag_url(id: SecureRandom.uuid),
|
patch api_v1_tag_url(id: SecureRandom.uuid),
|
||||||
params: { tag: { name: "Not Found" } },
|
params: { tag: { name: "Not Found" } },
|
||||||
headers: read_write_headers
|
headers: read_write_headers
|
||||||
|
|
||||||
assert_response :not_found
|
assert_response :not_found
|
||||||
end
|
end
|
||||||
|
|
||||||
test "update returns 404 for tag from another family" do
|
test "update returns 404 for tag from another family" do
|
||||||
other_tag = @other_family_user.family.tags.create!(name: "Other Tag", color: "#3b82f6")
|
other_tag = @other_family_user.family.tags.create!(name: "Other Tag", color: "#3b82f6")
|
||||||
|
|
||||||
patch api_v1_tag_url(other_tag),
|
patch api_v1_tag_url(other_tag),
|
||||||
params: { tag: { name: "Hacker Update" } },
|
params: { tag: { name: "Hacker Update" } },
|
||||||
headers: read_write_headers
|
headers: read_write_headers
|
||||||
|
|
||||||
assert_response :not_found
|
assert_response :not_found
|
||||||
end
|
end
|
||||||
|
|
||||||
# Destroy action tests
|
# Destroy action tests
|
||||||
test "destroy requires authentication" do
|
test "destroy requires authentication" do
|
||||||
delete api_v1_tag_url(@tag)
|
delete api_v1_tag_url(@tag)
|
||||||
|
|
||||||
assert_response :unauthorized
|
assert_response :unauthorized
|
||||||
end
|
end
|
||||||
|
|
||||||
test "destroy requires read_write scope" do
|
test "destroy requires read_write scope" do
|
||||||
delete api_v1_tag_url(@tag), headers: read_headers
|
delete api_v1_tag_url(@tag), headers: read_headers
|
||||||
|
|
||||||
assert_response :forbidden
|
assert_response :forbidden
|
||||||
end
|
end
|
||||||
|
|
||||||
test "destroy tag successfully" do
|
test "destroy tag successfully" do
|
||||||
tag_to_delete = @user.family.tags.create!(name: "Delete Me #{SecureRandom.hex(4)}", color: "#c44fe9")
|
tag_to_delete = @user.family.tags.create!(name: "Delete Me #{SecureRandom.hex(4)}", color: "#c44fe9")
|
||||||
|
|
||||||
assert_difference -> { @user.family.tags.count }, -1 do
|
assert_difference -> { @user.family.tags.count }, -1 do
|
||||||
delete api_v1_tag_url(tag_to_delete), headers: read_write_headers
|
delete api_v1_tag_url(tag_to_delete), headers: read_write_headers
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_response :no_content
|
assert_response :no_content
|
||||||
end
|
end
|
||||||
|
|
||||||
test "destroy returns 404 for non-existent tag" do
|
test "destroy returns 404 for non-existent tag" do
|
||||||
delete api_v1_tag_url(id: SecureRandom.uuid), headers: read_write_headers
|
delete api_v1_tag_url(id: SecureRandom.uuid), headers: read_write_headers
|
||||||
|
|
||||||
assert_response :not_found
|
assert_response :not_found
|
||||||
end
|
end
|
||||||
|
|
||||||
test "destroy returns 404 for tag from another family" do
|
test "destroy returns 404 for tag from another family" do
|
||||||
other_tag = @other_family_user.family.tags.create!(name: "Other Tag", color: "#3b82f6")
|
other_tag = @other_family_user.family.tags.create!(name: "Other Tag", color: "#3b82f6")
|
||||||
|
|
||||||
assert_no_difference -> { @other_family_user.family.tags.count } do
|
assert_no_difference -> { @other_family_user.family.tags.count } do
|
||||||
delete api_v1_tag_url(other_tag), headers: read_write_headers
|
delete api_v1_tag_url(other_tag), headers: read_write_headers
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_response :not_found
|
assert_response :not_found
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def read_headers
|
def read_headers
|
||||||
{ "Authorization" => "Bearer #{@read_token.token}" }
|
{ "Authorization" => "Bearer #{@read_token.token}" }
|
||||||
end
|
end
|
||||||
|
|
||||||
def read_write_headers
|
def read_write_headers
|
||||||
{ "Authorization" => "Bearer #{@read_write_token.token}" }
|
{ "Authorization" => "Bearer #{@read_write_token.token}" }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user