gitforge/services/issue/reaction_test.go
limiting-factor a48683f064 feat: add internal APIs for reactions notifications (#13017)
- add the NewReaction and DeleteReaction interface and loop to the
  notify service
- use services/issue instead of models/issues in REST and web API to
  create and delete reactions
- Issues and Comment reactions created via services/issue notify via NewReaction
- Issues and Comment reactions deleted via services/issue notify via DeleteReaction

### Tests for Go changes

- I added test coverage for Go changes...
  - [x] in their respective `*_test.go` for unit tests.
  - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server.
- I ran...
  - [x] `make pr-go` before pushing

### Documentation

- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it.

### Release notes

- [ ] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change.
- [x] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/13017
Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org>
2026-06-29 03:59:39 +02:00

105 lines
3.2 KiB
Go

// Copyright 2026 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
package issue_test
import (
"context"
"testing"
issues_model "forgejo.org/models/issues"
"forgejo.org/models/unittest"
user_model "forgejo.org/models/user"
issue_service "forgejo.org/services/issue"
notify_service "forgejo.org/services/notify"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type reactionNotifier struct {
notify_service.NullNotifier
new []*issues_model.Reaction
deleted []*issues_model.Reaction
}
func (o *reactionNotifier) NewReaction(ctx context.Context, reaction *issues_model.Reaction) {
o.new = append(o.new, reaction)
}
func (o *reactionNotifier) DeleteReaction(ctx context.Context, reaction *issues_model.Reaction) {
o.deleted = append(o.deleted, reaction)
}
func TestServicesReaction(t *testing.T) {
require.NoError(t, unittest.PrepareTestDatabase())
reactionEqual := func(t *testing.T, a, b *issues_model.Reaction) {
t.Helper()
assert.Equal(t, a.Type, b.Type)
assert.Equal(t, a.UserID, b.UserID)
assert.Equal(t, a.IssueID, b.IssueID)
assert.Equal(t, a.CommentID, b.CommentID)
}
t.Run("CommentReaction", func(t *testing.T) {
unittest.LoadFixtures()
notifier := &reactionNotifier{}
notify_service.RegisterNotifier(notifier)
defer notify_service.UnregisterNotifier(notifier)
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 2})
require.NoError(t, comment.LoadIssue(t.Context()))
issue := comment.Issue
content := "+1"
expectedReaction := &issues_model.Reaction{
Type: content,
UserID: user.ID,
IssueID: issue.ID,
CommentID: comment.ID,
}
reaction, err := issue_service.CreateCommentReaction(t.Context(), user, issue, comment, content)
require.NoError(t, err)
reactionEqual(t, expectedReaction, reaction)
require.Len(t, notifier.new, 1)
reactionEqual(t, expectedReaction, notifier.new[0])
require.Empty(t, notifier.deleted, 0)
require.NoError(t, issue_service.DeleteCommentReaction(t.Context(), user, comment, content))
require.Len(t, notifier.new, 1)
require.Len(t, notifier.deleted, 1)
reactionEqual(t, expectedReaction, notifier.deleted[0])
})
t.Run("IssueReaction", func(t *testing.T) {
unittest.LoadFixtures()
notifier := &reactionNotifier{}
notify_service.RegisterNotifier(notifier)
defer notify_service.UnregisterNotifier(notifier)
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2})
content := "+1"
expectedReaction := &issues_model.Reaction{
Type: content,
UserID: user.ID,
IssueID: issue.ID,
}
reaction, err := issue_service.CreateIssueReaction(t.Context(), user, issue, content)
require.NoError(t, err)
reactionEqual(t, expectedReaction, reaction)
require.Len(t, notifier.new, 1)
reactionEqual(t, expectedReaction, notifier.new[0])
require.Empty(t, notifier.deleted, 0)
require.NoError(t, issue_service.DeleteIssueReaction(t.Context(), user, issue, content))
require.Len(t, notifier.new, 1)
require.Len(t, notifier.deleted, 1)
reactionEqual(t, expectedReaction, notifier.deleted[0])
})
}