James Moger
2011-12-07 7e8873a14ccc2cb25213489d7d7ba97f09673831
commit | author | age
93f472 1 /*
JM 2  * Copyright 2011 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.gitblit.tests;
17
7e8873 18 import static org.junit.Assert.assertEquals;
JM 19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertTrue;
21
93f472 22 import java.io.File;
JM 23 import java.io.IOException;
24
7e8873 25 import org.junit.Test;
93f472 26
JM 27 import com.gitblit.ConfigUserService;
28 import com.gitblit.FileUserService;
29 import com.gitblit.IUserService;
7e8873 30 import com.gitblit.models.TeamModel;
93f472 31 import com.gitblit.models.UserModel;
JM 32
7e8873 33 public class UserServiceTest {
93f472 34
7e8873 35     @Test
93f472 36     public void testFileUserService() throws IOException {
JM 37         File file = new File("us-test.properties");
38         file.delete();
7e8873 39         IUserService service = new FileUserService(file);
JM 40         testUsers(service);
93f472 41         file.delete();
JM 42     }
43
7e8873 44     @Test
93f472 45     public void testConfigUserService() throws IOException {
JM 46         File file = new File("us-test.conf");
47         file.delete();
7e8873 48         IUserService service = new ConfigUserService(file);
JM 49         testUsers(service);
93f472 50         file.delete();
JM 51     }
52
7e8873 53     protected void testUsers(IUserService service) {
93f472 54
JM 55         UserModel admin = service.getUserModel("admin");
56         assertTrue(admin == null);
57
58         // add admin
59         admin = new UserModel("admin");
60         admin.password = "password";
61         admin.canAdmin = true;
62         admin.excludeFromFederation = true;
63         service.updateUserModel(admin);
64         admin = null;
65
66         // add new user
67         UserModel newUser = new UserModel("test");
68         newUser.password = "testPassword";
69         newUser.addRepository("repo1");
70         newUser.addRepository("repo2");
71         newUser.addRepository("sub/repo3");
72         service.updateUserModel(newUser);
73
74         // add one more new user and then test reload of first new user
75         newUser = new UserModel("garbage");
76         newUser.password = "garbage";
77         service.updateUserModel(newUser);
78
79         // confirm all added users
80         assertEquals(3, service.getAllUsernames().size());
81
82         // confirm reloaded test user
83         newUser = service.getUserModel("test");
84         assertEquals("testPassword", newUser.password);
85         assertEquals(3, newUser.repositories.size());
86         assertTrue(newUser.hasRepository("repo1"));
87         assertTrue(newUser.hasRepository("repo2"));
88         assertTrue(newUser.hasRepository("sub/repo3"));
89
90         // confirm authentication of test user
91         UserModel testUser = service.authenticate("test", "testPassword".toCharArray());
92         assertEquals("test", testUser.username);
93         assertEquals("testPassword", testUser.password);
94
95         // delete a repository role and confirm role removal from test user
96         service.deleteRepositoryRole("repo2");
97         testUser = service.getUserModel("test");
98         assertEquals(2, testUser.repositories.size());
99
100         // delete garbage user and confirm user count
101         service.deleteUser("garbage");
102         assertEquals(2, service.getAllUsernames().size());
103
104         // rename repository and confirm role change for test user
105         service.renameRepositoryRole("repo1", "newrepo1");
106         testUser = service.getUserModel("test");
107         assertTrue(testUser.hasRepository("newrepo1"));
108     }
109 }