summaryrefslogtreecommitdiffstats
path: root/models/user/block_test.go
blob: a795ef345e919a8bfbea95cc6a1af4d6d2d6210a (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
// Copyright 2023 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package user_test

import (
	"testing"

	"code.gitea.io/gitea/models/db"
	"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 TestIsBlocked(t *testing.T) {
	require.NoError(t, unittest.PrepareTestDatabase())
	assert.True(t, user_model.IsBlocked(db.DefaultContext, 4, 1))

	// Simple test cases to ensure the function can also respond with false.
	assert.False(t, user_model.IsBlocked(db.DefaultContext, 1, 1))
	assert.False(t, user_model.IsBlocked(db.DefaultContext, 3, 2))
}

func TestIsBlockedMultiple(t *testing.T) {
	require.NoError(t, unittest.PrepareTestDatabase())
	assert.True(t, user_model.IsBlockedMultiple(db.DefaultContext, []int64{4}, 1))
	assert.True(t, user_model.IsBlockedMultiple(db.DefaultContext, []int64{4, 3, 4, 5}, 1))

	// Simple test cases to ensure the function can also respond with false.
	assert.False(t, user_model.IsBlockedMultiple(db.DefaultContext, []int64{1}, 1))
	assert.False(t, user_model.IsBlockedMultiple(db.DefaultContext, []int64{3, 4, 1}, 2))
}

func TestUnblockUser(t *testing.T) {
	require.NoError(t, unittest.PrepareTestDatabase())
	assert.True(t, user_model.IsBlocked(db.DefaultContext, 4, 1))

	require.NoError(t, user_model.UnblockUser(db.DefaultContext, 4, 1))

	// Simple test cases to ensure the function can also respond with false.
	assert.False(t, user_model.IsBlocked(db.DefaultContext, 4, 1))
}

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

	blockedUsers, err := user_model.ListBlockedUsers(db.DefaultContext, 4, db.ListOptions{})
	require.NoError(t, err)
	if assert.Len(t, blockedUsers, 1) {
		assert.EqualValues(t, 1, blockedUsers[0].ID)
		// The function returns the created Unix of the block, not that of the user.
		assert.EqualValues(t, 1671607299, blockedUsers[0].CreatedUnix)
	}
}

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

	blockedByUserIDs, err := user_model.ListBlockedByUsersID(db.DefaultContext, 1)
	require.NoError(t, err)
	if assert.Len(t, blockedByUserIDs, 1) {
		assert.EqualValues(t, 4, blockedByUserIDs[0])
	}
}

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

	count, err := user_model.CountBlockedUsers(db.DefaultContext, 4)
	require.NoError(t, err)
	assert.EqualValues(t, 1, count)

	count, err = user_model.CountBlockedUsers(db.DefaultContext, 1)
	require.NoError(t, err)
	assert.EqualValues(t, 0, count)
}