summaryrefslogtreecommitdiffstats
path: root/models/userlist_test.go
blob: c48cfb61c168b435ac2e8b0959598a9f2becf8e2 (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
87
88
89
90
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package models

import (
	"fmt"
	"testing"

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

func TestUserListIsPublicMember(t *testing.T) {
	assert.NoError(t, PrepareTestDatabase())
	tt := []struct {
		orgid    int64
		expected map[int64]bool
	}{
		{3, map[int64]bool{2: true, 4: false, 28: true}},
		{6, map[int64]bool{5: true, 28: true}},
		{7, map[int64]bool{5: false}},
		{25, map[int64]bool{24: true}},
		{22, map[int64]bool{}},
	}
	for _, v := range tt {
		t.Run(fmt.Sprintf("IsPublicMemberOfOrdIg%d", v.orgid), func(t *testing.T) {
			testUserListIsPublicMember(t, v.orgid, v.expected)
		})
	}
}
func testUserListIsPublicMember(t *testing.T, orgID int64, expected map[int64]bool) {
	org, err := GetUserByID(orgID)
	assert.NoError(t, err)
	assert.NoError(t, org.GetMembers())
	assert.Equal(t, expected, org.MembersIsPublic)

}

func TestUserListIsUserOrgOwner(t *testing.T) {
	assert.NoError(t, PrepareTestDatabase())
	tt := []struct {
		orgid    int64
		expected map[int64]bool
	}{
		{3, map[int64]bool{2: true, 4: false, 28: false}},
		{6, map[int64]bool{5: true, 28: false}},
		{7, map[int64]bool{5: true}},
		{25, map[int64]bool{24: false}}, // ErrTeamNotExist
		{22, map[int64]bool{}},          // No member
	}
	for _, v := range tt {
		t.Run(fmt.Sprintf("IsUserOrgOwnerOfOrdIg%d", v.orgid), func(t *testing.T) {
			testUserListIsUserOrgOwner(t, v.orgid, v.expected)
		})
	}
}

func testUserListIsUserOrgOwner(t *testing.T, orgID int64, expected map[int64]bool) {
	org, err := GetUserByID(orgID)
	assert.NoError(t, err)
	assert.NoError(t, org.GetMembers())
	assert.Equal(t, expected, org.Members.IsUserOrgOwner(orgID))
}

func TestUserListIsTwoFaEnrolled(t *testing.T) {
	assert.NoError(t, PrepareTestDatabase())
	tt := []struct {
		orgid    int64
		expected map[int64]bool
	}{
		{3, map[int64]bool{2: false, 4: false, 28: false}},
		{6, map[int64]bool{5: false, 28: false}},
		{7, map[int64]bool{5: false}},
		{25, map[int64]bool{24: true}},
		{22, map[int64]bool{}},
	}
	for _, v := range tt {
		t.Run(fmt.Sprintf("IsTwoFaEnrolledOfOrdIg%d", v.orgid), func(t *testing.T) {
			testUserListIsTwoFaEnrolled(t, v.orgid, v.expected)
		})
	}
}

func testUserListIsTwoFaEnrolled(t *testing.T, orgID int64, expected map[int64]bool) {
	org, err := GetUserByID(orgID)
	assert.NoError(t, err)
	assert.NoError(t, org.GetMembers())
	assert.Equal(t, expected, org.Members.GetTwoFaStatus())
}