Browse Source

Unit tests for models/repo_collaboration (#1032)

Ethan Koenig 2 years ago
parent
commit
f1ab906c51
2 changed files with 106 additions and 1 deletions
  1. 1 1
      models/fixtures/access.yml
  2. 105 0
      models/repo_collaboration_test.go

+ 1 - 1
models/fixtures/access.yml

@@ -6,6 +6,6 @@
6 6
 
7 7
 -
8 8
   id: 2
9
-  repo_id: 4
10 9
   user_id: 4
10
+  repo_id: 4
11 11
   mode: 2 # write

+ 105 - 0
models/repo_collaboration_test.go

@@ -0,0 +1,105 @@
1
+// Copyright 2017 The Gitea Authors. All rights reserved.
2
+// Use of this source code is governed by a MIT-style
3
+// license that can be found in the LICENSE file.
4
+
5
+package models
6
+
7
+import (
8
+	"testing"
9
+
10
+	"github.com/stretchr/testify/assert"
11
+)
12
+
13
+func TestCollaboration_ModeI18nKey(t *testing.T) {
14
+	assert.Equal(t, "repo.settings.collaboration.read",
15
+		(&Collaboration{Mode: AccessModeRead}).ModeI18nKey())
16
+	assert.Equal(t, "repo.settings.collaboration.write",
17
+		(&Collaboration{Mode: AccessModeWrite}).ModeI18nKey())
18
+	assert.Equal(t, "repo.settings.collaboration.admin",
19
+		(&Collaboration{Mode: AccessModeAdmin}).ModeI18nKey())
20
+	assert.Equal(t, "repo.settings.collaboration.undefined",
21
+		(&Collaboration{Mode: AccessModeNone}).ModeI18nKey())
22
+}
23
+
24
+func TestRepository_AddCollaborator(t *testing.T) {
25
+	assert.NoError(t, PrepareTestDatabase())
26
+
27
+	testSuccess := func(repoID, userID int64) {
28
+		repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
29
+		assert.NoError(t, repo.GetOwner())
30
+		user := AssertExistsAndLoadBean(t, &User{ID: userID}).(*User)
31
+		assert.NoError(t, repo.AddCollaborator(user))
32
+		CheckConsistencyFor(t, &Repository{ID: repoID}, &User{ID: userID})
33
+	}
34
+	testSuccess(1, 4)
35
+	testSuccess(1, 4)
36
+	testSuccess(3, 4)
37
+}
38
+
39
+func TestRepository_GetCollaborators(t *testing.T) {
40
+	assert.NoError(t, PrepareTestDatabase())
41
+	test := func(repoID int64) {
42
+		repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
43
+		collaborators, err := repo.GetCollaborators()
44
+		assert.NoError(t, err)
45
+		expectedLen, err := x.Count(&Collaboration{RepoID: repoID})
46
+		assert.NoError(t, err)
47
+		assert.Len(t, collaborators, int(expectedLen))
48
+		for _, collaborator := range collaborators {
49
+			assert.EqualValues(t, collaborator.User.ID, collaborator.Collaboration.UserID)
50
+			assert.EqualValues(t, repoID, collaborator.Collaboration.RepoID)
51
+		}
52
+	}
53
+	test(1)
54
+	test(2)
55
+	test(3)
56
+	test(4)
57
+}
58
+
59
+func TestRepository_IsCollaborator(t *testing.T) {
60
+	assert.NoError(t, PrepareTestDatabase())
61
+
62
+	test := func(repoID, userID int64, expected bool) {
63
+		repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
64
+		actual, err := repo.IsCollaborator(userID)
65
+		assert.NoError(t, err)
66
+		assert.Equal(t, expected, actual)
67
+	}
68
+	test(3, 2, true)
69
+	test(3, NonexistentID, false)
70
+	test(4, 2, false)
71
+	test(4, 4, true)
72
+}
73
+
74
+func TestRepository_ChangeCollaborationAccessMode(t *testing.T) {
75
+	assert.NoError(t, PrepareTestDatabase())
76
+
77
+	repo := AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository)
78
+	assert.NoError(t, repo.ChangeCollaborationAccessMode(4, AccessModeAdmin))
79
+
80
+	collaboration := AssertExistsAndLoadBean(t, &Collaboration{RepoID: repo.ID, UserID: 4}).(*Collaboration)
81
+	assert.EqualValues(t, AccessModeAdmin, collaboration.Mode)
82
+
83
+	access := AssertExistsAndLoadBean(t, &Access{UserID: 4, RepoID: repo.ID}).(*Access)
84
+	assert.EqualValues(t, AccessModeAdmin, access.Mode)
85
+
86
+	assert.NoError(t, repo.ChangeCollaborationAccessMode(4, AccessModeAdmin))
87
+
88
+	assert.NoError(t, repo.ChangeCollaborationAccessMode(NonexistentID, AccessModeAdmin))
89
+
90
+	CheckConsistencyFor(t, &Repository{ID: repo.ID})
91
+}
92
+
93
+func TestRepository_DeleteCollaboration(t *testing.T) {
94
+	assert.NoError(t, PrepareTestDatabase())
95
+
96
+	repo := AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository)
97
+	assert.NoError(t, repo.GetOwner())
98
+	assert.NoError(t, repo.DeleteCollaboration(4))
99
+	AssertNotExistsBean(t, &Collaboration{RepoID: repo.ID, UserID: 4})
100
+
101
+	assert.NoError(t, repo.DeleteCollaboration(4))
102
+	AssertNotExistsBean(t, &Collaboration{RepoID: repo.ID, UserID: 4})
103
+
104
+	CheckConsistencyFor(t, &Repository{ID: repo.ID})
105
+}