summaryrefslogtreecommitdiffstats
path: root/models/issues/comment_list_test.go
blob: 5ad1cd19c921eeb18e6bbcc946ae89de9c9ea3e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2024 The Forgejo Authors
// SPDX-License-Identifier: MIT

package issues

import (
	"testing"

	"code.gitea.io/gitea/models/db"
	repo_model "code.gitea.io/gitea/models/repo"
	"code.gitea.io/gitea/models/unittest"
	user_model "code.gitea.io/gitea/models/user"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestCommentListLoadUser(t *testing.T) {
	require.NoError(t, unittest.PrepareTestDatabase())

	issue := unittest.AssertExistsAndLoadBean(t, &Issue{})
	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID})
	doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})

	for _, testCase := range []struct {
		poster   int64
		assignee int64
		user     *user_model.User
	}{
		{
			poster:   user_model.ActionsUserID,
			assignee: user_model.ActionsUserID,
			user:     user_model.NewActionsUser(),
		},
		{
			poster:   user_model.GhostUserID,
			assignee: user_model.GhostUserID,
			user:     user_model.NewGhostUser(),
		},
		{
			poster:   doer.ID,
			assignee: doer.ID,
			user:     doer,
		},
		{
			poster:   0,
			assignee: 0,
			user:     user_model.NewGhostUser(),
		},
		{
			poster:   -200,
			assignee: -200,
			user:     user_model.NewGhostUser(),
		},
		{
			poster:   200,
			assignee: 200,
			user:     user_model.NewGhostUser(),
		},
	} {
		t.Run(testCase.user.Name, func(t *testing.T) {
			comment, err := CreateComment(db.DefaultContext, &CreateCommentOptions{
				Type:    CommentTypeComment,
				Doer:    testCase.user,
				Repo:    repo,
				Issue:   issue,
				Content: "Hello",
			})
			assert.NoError(t, err)

			list := CommentList{comment}

			comment.PosterID = testCase.poster
			comment.Poster = nil
			assert.NoError(t, list.LoadPosters(db.DefaultContext))
			require.NotNil(t, comment.Poster)
			assert.Equal(t, testCase.user.ID, comment.Poster.ID)

			comment.AssigneeID = testCase.assignee
			comment.Assignee = nil
			require.NoError(t, list.loadAssignees(db.DefaultContext))
			require.NotNil(t, comment.Assignee)
			assert.Equal(t, testCase.user.ID, comment.Assignee.ID)
		})
	}
}