James Moger
2012-10-01 eb1405f736f2f98e14215774dd53eea9b9a77017
commit | author | age
831469 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;
997c16 19 import static org.junit.Assert.assertNotNull;
JM 20 import static org.junit.Assert.assertTrue;
7e8873 21
831469 22 import java.util.Date;
JM 23 import java.util.HashMap;
997c16 24 import java.util.List;
831469 25 import java.util.Map;
7e8873 26 import java.util.concurrent.atomic.AtomicBoolean;
831469 27
7e8873 28 import org.junit.AfterClass;
JM 29 import org.junit.BeforeClass;
30 import org.junit.Test;
831469 31
JM 32 import com.gitblit.Constants.AccessRestrictionType;
4aafd4 33 import com.gitblit.Constants.FederationProposalResult;
831469 34 import com.gitblit.Constants.FederationRequest;
JM 35 import com.gitblit.Constants.FederationToken;
997c16 36 import com.gitblit.models.FederationModel;
dd9ae7 37 import com.gitblit.models.FederationProposal;
831469 38 import com.gitblit.models.RepositoryModel;
997c16 39 import com.gitblit.models.TeamModel;
JM 40 import com.gitblit.models.UserModel;
831469 41 import com.gitblit.utils.FederationUtils;
93f0b1 42 import com.gitblit.utils.JsonUtils;
997c16 43 import com.gitblit.utils.RpcUtils;
831469 44
7e8873 45 public class FederationTests {
831469 46
7e8873 47     String url = GitBlitSuite.url;
JM 48     String account = GitBlitSuite.account;
49     String password = GitBlitSuite.password;
997c16 50     String token = "d7cc58921a80b37e0329a4dae2f9af38bf61ef5c";
831469 51
7e8873 52     private static final AtomicBoolean started = new AtomicBoolean(false);
831469 53
7e8873 54     @BeforeClass
JM 55     public static void startGitblit() throws Exception {
56         started.set(GitBlitSuite.startGitblit());
831469 57     }
JM 58
7e8873 59     @AfterClass
JM 60     public static void stopGitblit() throws Exception {
61         if (started.get()) {
62             GitBlitSuite.stopGitblit();
63         }
831469 64     }
JM 65
7e8873 66     @Test
831469 67     public void testProposal() throws Exception {
JM 68         // create dummy repository data
69         Map<String, RepositoryModel> repositories = new HashMap<String, RepositoryModel>();
70         for (int i = 0; i < 5; i++) {
71             RepositoryModel model = new RepositoryModel();
72             model.accessRestriction = AccessRestrictionType.VIEW;
73             model.description = "cloneable repository " + i;
74             model.lastChange = new Date();
75             model.owner = "adminuser";
76             model.name = "repo" + i + ".git";
77             model.size = "5 MB";
78             model.hasCommits = true;
79             repositories.put(model.name, model);
80         }
81
dd9ae7 82         FederationProposal proposal = new FederationProposal("http://testurl", FederationToken.ALL,
JM 83                 "testtoken", repositories);
84
831469 85         // propose federation
7e8873 86         assertEquals("proposal refused", FederationUtils.propose(url, proposal),
4aafd4 87                 FederationProposalResult.NO_PROPOSALS);
831469 88     }
JM 89
7e8873 90     @Test
997c16 91     public void testJsonRepositories() throws Exception {
JM 92         String requrl = FederationUtils.asLink(url, token, FederationRequest.PULL_REPOSITORIES);
93         String json = JsonUtils.retrieveJsonString(requrl, null, null);
94         assertNotNull(json);
95     }
96
97     @Test
98     public void testJsonUsers() throws Exception {
99         String requrl = FederationUtils.asLink(url, token, FederationRequest.PULL_USERS);
100         String json = JsonUtils.retrieveJsonString(requrl, null, null);
101         assertNotNull(json);
102     }
103
104     @Test
105     public void testJsonTeams() throws Exception {
106         String requrl = FederationUtils.asLink(url, token, FederationRequest.PULL_TEAMS);
107         String json = JsonUtils.retrieveJsonString(requrl, null, null);
108         assertNotNull(json);
109     }
110
111     private FederationModel getRegistration() {
112         FederationModel model = new FederationModel("localhost");
113         model.url = this.url;
114         model.token = this.token;
115         return model;
116     }
117
118     @Test
831469 119     public void testPullRepositories() throws Exception {
997c16 120         Map<String, RepositoryModel> repos = FederationUtils.getRepositories(getRegistration(),
JM 121                 false);
122         assertNotNull(repos);
123         assertTrue(repos.size() > 0);
124     }
125
126     @Test
127     public void testPullUsers() throws Exception {
128         List<UserModel> users = FederationUtils.getUsers(getRegistration());
129         assertNotNull(users);
130         // admin is excluded
131         assertEquals(0, users.size());
132         
133         UserModel newUser = new UserModel("test");
134         newUser.password = "whocares";
135         assertTrue(RpcUtils.createUser(newUser, url, account, password.toCharArray()));
136         
137         TeamModel team = new TeamModel("testteam");
138         team.addUser("test");
139         team.addRepository("helloworld.git");
140         assertTrue(RpcUtils.createTeam(team, url, account, password.toCharArray()));
141         
142         users = FederationUtils.getUsers(getRegistration());
143         assertNotNull(users);
144         assertEquals(1, users.size());
145         
146         newUser = users.get(0);
147         assertTrue(newUser.isTeamMember("testteam"));        
148         
149         assertTrue(RpcUtils.deleteUser(newUser, url, account, password.toCharArray()));
150         assertTrue(RpcUtils.deleteTeam(team, url, account, password.toCharArray()));
151     }
152
153     @Test
154     public void testPullTeams() throws Exception {
155         List<TeamModel> teams = FederationUtils.getTeams(getRegistration());
156         assertNotNull(teams);
157         assertTrue(teams.size() > 0);
831469 158     }
df162c 159     
JM 160     @Test
161     public void testPullScripts() throws Exception {
162         Map<String, String> scripts = FederationUtils.getScripts(getRegistration());
163         assertNotNull(scripts);
164         assertTrue(scripts.keySet().contains("sendmail"));
165     }
831469 166 }