Skip to content

HandlerのAnnouncementのテスト実装#5

Merged
hikaru-0602 merged 19 commits intomainfrom
feature/handler-announcement-test
Feb 10, 2026
Merged

HandlerのAnnouncementのテスト実装#5
hikaru-0602 merged 19 commits intomainfrom
feature/handler-announcement-test

Conversation

@hikaru-0602
Copy link
Copy Markdown
Contributor

やったこと

  • RepositoryのAnnouncementのモック作成
  • HandlerのAnnouncementのテスト実装

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements comprehensive test coverage for the Announcement handler and creates a mock repository for testing purposes. The implementation adds handler tests for all CRUD operations (List, Detail, Create, Update, Delete) with authentication verification, along with a mock repository that returns predictable test data.

Changes:

  • Created a mock implementation of AnnouncementRepository for testing
  • Added comprehensive handler tests for all announcement endpoints with table-driven test patterns
  • Added testify library as a test dependency

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 19 comments.

File Description
internal/repository/announcement_mock.go Mock repository implementation that returns predictable test data for all CRUD operations
internal/handler/announcement_test.go Comprehensive test suite for announcement handlers covering basic functionality and authentication
go.mod Added testify testing library dependency and its transitive dependencies

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Member

@kantacky kantacky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilotのレビュー直せば良さそう
403のケースとか、カスタムクレームの組み合わせのケースとか

Copy link
Copy Markdown
Member

@kantacky kantacky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

間違えてApproveしちゃった

Enhanced the TestAnnouncementsV1List function to include a new test case for handling requests with a developer claim. Updated the test structure to accommodate the additional claim and ensured proper validation of the response for this scenario.
Enhanced the TestAnnouncementsV1Update function by adding a new test case that verifies the response when a token with non-admin/developer claims is used. This ensures proper handling of insufficient permissions and improves overall test coverage for authentication scenarios.
Enhanced the TestAnnouncementsV1Create function by adding a new test case that verifies the ability to create announcements with a developer claim. Updated the test structure to include the new claim and ensured proper validation of the response for this scenario.
Updated the TestAnnouncementsV1Delete function to include a new test case for handling deletion requests with a developer claim. Adjusted the expected status code to reflect the correct response for successful deletions and modified the test setup to accommodate different claim types. Additionally, changed the handler to use AbortWithStatus for consistency in response handling.
Updated the TestAnnouncementsV1List and TestAnnouncementsV1Update functions to enhance test clarity and error handling. Introduced the require package for better error assertions and adjusted variable formatting for consistency. This improves overall test reliability and readability.
Copilot AI review requested due to automatic review settings February 8, 2026 06:21
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +96 to +97
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
},
Copy link

Copilot AI Feb 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Content-Type を完全一致で application/json; charset=utf-8 に固定すると、Gin の挙動変更やミドルウェアによる差分でテストが壊れやすいです。mime.ParseMediaType で media type 部分だけを確認するか、strings.HasPrefix(contentType, "application/json") のように JSON であることを検証する形がより堅牢です。

Copilot uses AI. Check for mistakes.
Copilot AI review requested due to automatic review settings February 8, 2026 08:09
@hikaru-0602 hikaru-0602 requested a review from kantacky February 8, 2026 08:11
@hikaru-0602
Copy link
Copy Markdown
Contributor Author

Copilotの指摘を修正し,全テストが通ることを確認しています

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@masaya-osuga
Copy link
Copy Markdown
Member

新しくきたCopilotの指摘を直したらいいと思います

@kantacky
Copy link
Copy Markdown
Member

@hikaru-0602
Copilotのコメント解決してる?してたら、Resolveボタン押してほしい
あと、コンフリクトしちゃったので、その修正もお願いします 🙇

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings February 10, 2026 02:06
@hikaru-0602
Copy link
Copy Markdown
Contributor Author

@kantacky
ごめん忘れてた.
コンフリクトも修正した

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@kantacky
Copy link
Copy Markdown
Member

@hikaru-0602
Copilotのコメント、まだ残ってそう?
あとCI落ちた...

…on validation

Updated TestAnnouncementsV1List, TestAnnouncementsV1Detail, and TestAnnouncementsV1Delete to include custom claims for testing 403 error scenarios. This allows for more granular permission checks and improves test coverage for authorization logic.
Copilot AI review requested due to automatic review settings February 10, 2026 02:23
@hikaru-0602
Copy link
Copy Markdown
Contributor Author

@kantacky
マージしてよい?

Copy link
Copy Markdown
Member

@kantacky kantacky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kantacky
Copy link
Copy Markdown
Member

kantacky commented Feb 10, 2026

まだコメント残ってるけど...
とりあえず影響なさそうだしいいや

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +128 to +129
assert.False(t, response.Announcements[0].AvailableFrom.IsZero(), "AvailableFromが設定されていること")
assert.False(t, response.Announcements[0].AvailableUntil.IsZero(), "AvailableUntilが設定されていること")
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

api.AnnouncementServiceAnnouncement.AvailableUntil*time.Time なので、response.Announcements[0].AvailableUntil.IsZero() はコンパイルできません。AvailableUntil != nil の確認をした上で response.Announcements[0].AvailableUntil.IsZero() を呼ぶ(もしくは *response... をデリファレンスして IsZero())形に修正してください。

Suggested change
assert.False(t, response.Announcements[0].AvailableFrom.IsZero(), "AvailableFromが設定されていること")
assert.False(t, response.Announcements[0].AvailableUntil.IsZero(), "AvailableUntilが設定されていること")
assert.False(t, response.Announcements[0].AvailableFrom.IsZero(), "AvailableFromが設定されていること")
assert.NotNil(t, response.Announcements[0].AvailableUntil, "AvailableUntilが設定されていること")
assert.False(t, response.Announcements[0].AvailableUntil.IsZero(), "AvailableUntilがゼロ時刻でないこと")

Copilot uses AI. Check for mistakes.
Comment on lines +182 to +206
func TestAnnouncementsV1Detail(t *testing.T) {
tests := []struct {
name string
id string
withAdminClaim bool
customClaims map[string]interface{} // 指定時はこのクレームでトークンをセット(403検証用)
wantCode int
validate func(t *testing.T, w *httptest.ResponseRecorder)
}{
{
name: "正常にお知らせ詳細が取得できる",
id: "1",
withAdminClaim: true,
wantCode: http.StatusOK,
validate: func(t *testing.T, w *httptest.ResponseRecorder) {
var response struct {
Announcement api.AnnouncementServiceAnnouncement `json:"announcement"`
}
err := json.Unmarshal(w.Body.Bytes(), &response)
assert.NoError(t, err, "JSONのパースに失敗しました")
assert.Equal(t, "1", response.Announcement.Id)
assert.Equal(t, "お知らせ1", response.Announcement.Title)
assert.Equal(t, "https://example.com/1", response.Announcement.Url)
},
},
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AnnouncementsV1Detail はハンドラ側で RequireAnyClaim(c, "admin", "developer") を使っているので、List/Create/Delete と同様に developer クレームのみで 200 になるケースもテストに追加したいです(現状 Detail は admin と 401/403 しか検証していません)。

Copilot uses AI. Check for mistakes.
Comment on lines +380 to +414
func TestAnnouncementsV1Update(t *testing.T) {
now := time.Now()
until := now.Add(24 * time.Hour)

tests := []struct {
name string
id string
request api.AnnouncementServiceAnnouncementRequest
withAdminClaim bool
customClaims map[string]interface{} // 指定時はこのクレームでトークンをセット(403検証用)
wantCode int
validate func(t *testing.T, w *httptest.ResponseRecorder)
}{
{
name: "正常にお知らせを更新できる",
id: "1",
request: api.AnnouncementServiceAnnouncementRequest{
Title: "更新されたお知らせ",
Url: "https://example.com/updated",
AvailableFrom: now,
AvailableUntil: &until,
},
withAdminClaim: true,
wantCode: http.StatusOK,
validate: func(t *testing.T, w *httptest.ResponseRecorder) {
var response struct {
Announcement api.AnnouncementServiceAnnouncement `json:"announcement"`
}
err := json.Unmarshal(w.Body.Bytes(), &response)
assert.NoError(t, err, "JSONのパースに失敗しました")
assert.Equal(t, "1", response.Announcement.Id)
assert.Equal(t, "更新されたお知らせ", response.Announcement.Title)
assert.Equal(t, "https://example.com/updated", response.Announcement.Url)
},
},
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AnnouncementsV1Update もハンドラ側で RequireAnyClaim(c, "admin", "developer") を使っているため、developer クレームのみで更新できる(200)ケースのテストが欲しいです。今のテストテーブルには developer 用の入力フラグがなく、admin と 401/403 のみになっています。

Copilot uses AI. Check for mistakes.
@hikaru-0602 hikaru-0602 merged commit 6eb6284 into main Feb 10, 2026
7 checks passed
@hikaru-0602 hikaru-0602 deleted the feature/handler-announcement-test branch February 10, 2026 02:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants