James Moger
2012-10-01 eb1405f736f2f98e14215774dd53eea9b9a77017
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 * Copyright 2011 gitblit.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitblit.tests;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
 
import java.io.File;
import java.io.IOException;
 
import org.junit.Test;
 
import com.gitblit.ConfigUserService;
import com.gitblit.FileUserService;
import com.gitblit.IUserService;
import com.gitblit.models.TeamModel;
import com.gitblit.models.UserModel;
 
public class UserServiceTest {
 
    @Test
    public void testFileUserService() throws IOException {
        File file = new File("us-test.properties");
        file.delete();
        IUserService service = new FileUserService(file);
        testUsers(service);
        testTeams(service);
        file.delete();
    }
 
    @Test
    public void testConfigUserService() throws IOException {
        File file = new File("us-test.conf");
        file.delete();
        IUserService service = new ConfigUserService(file);
        testUsers(service);
        testTeams(service);
        file.delete();
    }
 
    protected void testUsers(IUserService service) {
 
        UserModel admin = service.getUserModel("admin");
        assertTrue(admin == null);
 
        // add admin and admins team
        TeamModel admins = new TeamModel("admins");
        admins.mailingLists.add("admins@localhost.com");
        
        admin = new UserModel("admin");
        admin.password = "password";
        admin.canAdmin = true;
        admin.excludeFromFederation = true;
        admin.teams.add(admins);
        
        service.updateUserModel(admin);
        admin = null;
        admins = null;
 
        // add new user
        UserModel newUser = new UserModel("test");
        newUser.password = "testPassword";
        newUser.addRepository("repo1");
        newUser.addRepository("repo2");
        newUser.addRepository("sub/repo3");
        service.updateUserModel(newUser);
 
        // add one more new user and then test reload of first new user
        newUser = new UserModel("GARBAGE");
        newUser.password = "garbage";
        service.updateUserModel(newUser);
 
        // confirm all added users
        assertEquals(3, service.getAllUsernames().size());
        assertTrue(service.getUserModel("garbage") != null);
        assertTrue(service.getUserModel("GaRbAgE") != null);
        assertTrue(service.getUserModel("GARBAGE") != null);
 
        // confirm reloaded test user
        newUser = service.getUserModel("test");
        assertEquals("testPassword", newUser.password);
        assertEquals(3, newUser.repositories.size());
        assertTrue(newUser.hasRepository("repo1"));
        assertTrue(newUser.hasRepository("repo2"));
        assertTrue(newUser.hasRepository("sub/repo3"));
 
        // confirm authentication of test user
        UserModel testUser = service.authenticate("test", "testPassword".toCharArray());
        assertEquals("test", testUser.username);
        assertEquals("testPassword", testUser.password);
 
        // delete a repository role and confirm role removal from test user
        service.deleteRepositoryRole("repo2");
        testUser = service.getUserModel("test");
        assertEquals(2, testUser.repositories.size());
 
        // delete garbage user and confirm user count
        service.deleteUser("garbage");
        assertEquals(2, service.getAllUsernames().size());
 
        // rename repository and confirm role change for test user
        service.renameRepositoryRole("repo1", "newrepo1");
        testUser = service.getUserModel("test");
        assertTrue(testUser.hasRepository("newrepo1"));
    }
 
    protected void testTeams(IUserService service) {
 
        // confirm we have 1 team (admins)
        assertEquals(1, service.getAllTeamNames().size());
        assertEquals("admins", service.getAllTeamNames().get(0));
 
        // remove newrepo1 from test user
        // now test user has no repositories
        UserModel user = service.getUserModel("test");
        user.repositories.clear();
        service.updateUserModel(user);
        user = service.getUserModel("test");
        assertEquals(0, user.repositories.size());
        assertFalse(user.canAccessRepository("newrepo1"));
        assertFalse(user.canAccessRepository("NEWREPO1"));
 
        // create test team and add test user and newrepo1
        TeamModel team = new TeamModel("testteam");
        team.addUser("test");
        team.addRepository("newrepo1");
        service.updateTeamModel(team);
 
        // confirm 1 user and 1 repo
        team = service.getTeamModel("testteam");
        assertEquals(1, team.repositories.size());
        assertEquals(1, team.users.size());
 
        // confirm team membership
        user = service.getUserModel("test");
        assertEquals(0, user.repositories.size());
        assertEquals(1, user.teams.size());
 
        // confirm team access
        assertTrue(team.hasRepository("newrepo1"));
        assertTrue(user.hasTeamAccess("newrepo1"));
        assertTrue(team.hasRepository("NEWREPO1"));
        assertTrue(user.hasTeamAccess("NEWREPO1"));
 
        // rename the team and add new repository
        team.addRepository("newrepo2");
        team.name = "testteam2";
        service.updateTeamModel("testteam", team);
 
        team = service.getTeamModel("testteam2");
        user = service.getUserModel("test");
 
        // confirm user and team can access newrepo2
        assertEquals(2, team.repositories.size());
        assertTrue(team.hasRepository("newrepo2"));
        assertTrue(user.hasTeamAccess("newrepo2"));
        assertTrue(team.hasRepository("NEWREPO2"));
        assertTrue(user.hasTeamAccess("NEWREPO2"));
 
        // delete testteam2
        service.deleteTeam("testteam2");
        team = service.getTeamModel("testteam2");
        user = service.getUserModel("test");
 
        // confirm team does not exist and user can not access newrepo1 and 2
        assertEquals(null, team);
        assertFalse(user.canAccessRepository("newrepo1"));
        assertFalse(user.canAccessRepository("newrepo2"));
 
        // create new team and add it to user
        // this tests the inverse team creation/team addition
        team = new TeamModel("testteam");
        team.addRepository("NEWREPO1");
        team.addRepository("NEWREPO2");
        user.teams.add(team);
        service.updateUserModel(user);
 
        // confirm the inverted team addition
        user = service.getUserModel("test");
        team = service.getTeamModel("testteam");
        assertTrue(user.hasTeamAccess("newrepo1"));
        assertTrue(user.hasTeamAccess("newrepo2"));
        assertTrue(team.hasUser("test"));
 
        // drop testteam from user and add nextteam to user
        team = new TeamModel("nextteam");
        team.addRepository("NEWREPO1");
        team.addRepository("NEWREPO2");
        user.teams.clear();
        user.teams.add(team);
        service.updateUserModel(user);
 
        // confirm implicit drop
        user = service.getUserModel("test");
        team = service.getTeamModel("testteam");
        assertTrue(user.hasTeamAccess("newrepo1"));
        assertTrue(user.hasTeamAccess("newrepo2"));
        assertFalse(team.hasUser("test"));
        team = service.getTeamModel("nextteam");
        assertTrue(team.hasUser("test"));
 
        // delete the user and confirm team no longer has user
        service.deleteUser("test");
        team = service.getTeamModel("testteam");
        assertFalse(team.hasUser("test"));
 
        // delete both teams
        service.deleteTeam("testteam");
        service.deleteTeam("nextteam");
        
        // assert we still have the admins team
        assertEquals(1, service.getAllTeamNames().size());
        assertEquals("admins", service.getAllTeamNames().get(0));
        
        team = service.getTeamModel("admins");
        assertEquals(1, team.mailingLists.size());
        assertTrue(team.mailingLists.contains("admins@localhost.com"));
    }
}