@@ -10,6 +10,7 @@ import (
10
10
11
11
"code.gitea.io/gitea/models"
12
12
"code.gitea.io/gitea/modules/auth"
13
+ "code.gitea.io/gitea/modules/context"
13
14
"code.gitea.io/gitea/modules/test"
14
15
15
16
"github.com/stretchr/testify/assert"
@@ -59,3 +60,104 @@ func TestAddReadWriteOnlyDeployKey(t *testing.T) {
59
60
Mode : models .AccessModeWrite ,
60
61
})
61
62
}
63
+
64
+ func TestCollaborationPost (t * testing.T ) {
65
+
66
+ models .PrepareTestEnv (t )
67
+ ctx := test .MockContext (t , "user2/repo1/issues/labels" )
68
+ test .LoadUser (t , ctx , 2 )
69
+ test .LoadUser (t , ctx , 4 )
70
+ test .LoadRepo (t , ctx , 1 )
71
+
72
+ ctx .Req .Form .Set ("collaborator" , "user4" )
73
+
74
+ u := & models.User {
75
+ LowerName : "user2" ,
76
+ Type : models .UserTypeIndividual ,
77
+ }
78
+
79
+ re := & models.Repository {
80
+ ID : 2 ,
81
+ Owner : u ,
82
+ }
83
+
84
+ repo := & context.Repository {
85
+ Owner : u ,
86
+ Repository : re ,
87
+ }
88
+
89
+ ctx .Repo = repo
90
+
91
+ CollaborationPost (ctx )
92
+
93
+ assert .EqualValues (t , http .StatusFound , ctx .Resp .Status ())
94
+
95
+ exists , err := re .IsCollaborator (4 )
96
+ assert .NoError (t , err )
97
+ assert .True (t , exists )
98
+ }
99
+
100
+ func TestCollaborationPost_AddCollaboratorTwice (t * testing.T ) {
101
+
102
+ models .PrepareTestEnv (t )
103
+ ctx := test .MockContext (t , "user2/repo1/issues/labels" )
104
+ test .LoadUser (t , ctx , 2 )
105
+ test .LoadUser (t , ctx , 4 )
106
+ test .LoadRepo (t , ctx , 1 )
107
+
108
+ ctx .Req .Form .Set ("collaborator" , "user4" )
109
+
110
+ u := & models.User {
111
+ LowerName : "user2" ,
112
+ Type : models .UserTypeIndividual ,
113
+ }
114
+
115
+ re := & models.Repository {
116
+ ID : 2 ,
117
+ Owner : u ,
118
+ }
119
+
120
+ repo := & context.Repository {
121
+ Owner : u ,
122
+ Repository : re ,
123
+ }
124
+
125
+ ctx .Repo = repo
126
+
127
+ CollaborationPost (ctx )
128
+
129
+ assert .EqualValues (t , http .StatusFound , ctx .Resp .Status ())
130
+
131
+ exists , err := re .IsCollaborator (4 )
132
+ assert .NoError (t , err )
133
+ assert .True (t , exists )
134
+
135
+ // Try adding the same collaborator again
136
+ CollaborationPost (ctx )
137
+
138
+ assert .EqualValues (t , http .StatusFound , ctx .Resp .Status ())
139
+ assert .NotEmpty (t , ctx .Flash .ErrorMsg )
140
+ }
141
+
142
+ func TestCollaborationPost_NonExistentUser (t * testing.T ) {
143
+
144
+ models .PrepareTestEnv (t )
145
+ ctx := test .MockContext (t , "user2/repo1/issues/labels" )
146
+ test .LoadUser (t , ctx , 2 )
147
+ test .LoadRepo (t , ctx , 1 )
148
+
149
+ ctx .Req .Form .Set ("collaborator" , "user34" )
150
+
151
+ repo := & context.Repository {
152
+ Owner : & models.User {
153
+ LowerName : "user2" ,
154
+ },
155
+ }
156
+
157
+ ctx .Repo = repo
158
+
159
+ CollaborationPost (ctx )
160
+
161
+ assert .EqualValues (t , http .StatusFound , ctx .Resp .Status ())
162
+ assert .NotEmpty (t , ctx .Flash .ErrorMsg )
163
+ }
0 commit comments