1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.http.test;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertSame;
16 import static org.junit.Assert.fail;
17
18 import java.io.IOException;
19
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpServletRequestWrapper;
22
23 import org.eclipse.jetty.server.Request;
24 import org.eclipse.jgit.http.server.resolver.DefaultReceivePackFactory;
25 import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
26 import org.eclipse.jgit.lib.PersonIdent;
27 import org.eclipse.jgit.lib.Repository;
28 import org.eclipse.jgit.lib.StoredConfig;
29 import org.eclipse.jgit.transport.ReceivePack;
30 import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
31 import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
32 import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
33 import org.junit.Before;
34 import org.junit.Test;
35
36 public class DefaultReceivePackFactoryTest extends LocalDiskRepositoryTestCase {
37 private Repository db;
38
39 private ReceivePackFactory<HttpServletRequest> factory;
40
41 @Override
42 @Before
43 public void setUp() throws Exception {
44 super.setUp();
45
46 db = createBareRepository();
47 factory = new DefaultReceivePackFactory();
48 }
49
50 @SuppressWarnings("unchecked")
51 @Test
52 public void testDisabledSingleton() throws ServiceNotAuthorizedException {
53 factory = (ReceivePackFactory<HttpServletRequest>) ReceivePackFactory.DISABLED;
54
55 try {
56 factory.create(new R(null, "localhost"), db);
57 fail("Created session for anonymous user: null");
58 } catch (ServiceNotEnabledException e) {
59
60 }
61
62 try {
63 factory.create(new R("", "localhost"), db);
64 fail("Created session for anonymous user: \"\"");
65 } catch (ServiceNotEnabledException e) {
66
67 }
68
69 try {
70 factory.create(new R("bob", "localhost"), db);
71 fail("Created session for user: \"bob\"");
72 } catch (ServiceNotEnabledException e) {
73
74 }
75 }
76
77 @Test
78 public void testCreate_NullUser() throws ServiceNotEnabledException {
79 try {
80 factory.create(new R(null, "localhost"), db);
81 fail("Created session for anonymous user: null");
82 } catch (ServiceNotAuthorizedException e) {
83
84 }
85 }
86
87 @Test
88 public void testCreate_EmptyStringUser() throws ServiceNotEnabledException {
89 try {
90 factory.create(new R("", "localhost"), db);
91 fail("Created session for anonymous user: \"\"");
92 } catch (ServiceNotAuthorizedException e) {
93
94 }
95 }
96
97 @Test
98 public void testCreate_AuthUser() throws ServiceNotEnabledException,
99 ServiceNotAuthorizedException {
100 ReceivePack rp;
101 rp = factory.create(new R("bob", "1.2.3.4"), db);
102 assertNotNull("have ReceivePack", rp);
103 assertSame(db, rp.getRepository());
104
105 PersonIdent id = rp.getRefLogIdent();
106 assertNotNull(id);
107 assertEquals("bob", id.getName());
108 assertEquals("bob@1.2.3.4", id.getEmailAddress());
109
110
111 assertEquals(author.getTimeZoneOffset(), id.getTimeZoneOffset());
112 assertEquals(author.getWhen(), id.getWhen());
113 }
114
115 @Test
116 public void testCreate_Disabled() throws ServiceNotAuthorizedException,
117 IOException {
118 final StoredConfig cfg = db.getConfig();
119 cfg.setBoolean("http", null, "receivepack", false);
120 cfg.save();
121
122 try {
123 factory.create(new R(null, "localhost"), db);
124 fail("Created session for anonymous user: null");
125 } catch (ServiceNotEnabledException e) {
126
127 }
128
129 try {
130 factory.create(new R("", "localhost"), db);
131 fail("Created session for anonymous user: \"\"");
132 } catch (ServiceNotEnabledException e) {
133
134 }
135
136 try {
137 factory.create(new R("bob", "localhost"), db);
138 fail("Created session for user: \"bob\"");
139 } catch (ServiceNotEnabledException e) {
140
141 }
142 }
143
144 @Test
145 public void testCreate_Enabled() throws ServiceNotEnabledException,
146 ServiceNotAuthorizedException, IOException {
147 final StoredConfig cfg = db.getConfig();
148 cfg.setBoolean("http", null, "receivepack", true);
149 cfg.save();
150
151 ReceivePack rp;
152
153 rp = factory.create(new R(null, "1.2.3.4"), db);
154 assertNotNull("have ReceivePack", rp);
155 assertSame(db, rp.getRepository());
156
157 PersonIdent id = rp.getRefLogIdent();
158 assertNotNull(id);
159 assertEquals("anonymous", id.getName());
160 assertEquals("anonymous@1.2.3.4", id.getEmailAddress());
161
162
163 assertEquals(author.getTimeZoneOffset(), id.getTimeZoneOffset());
164 assertEquals(author.getWhen(), id.getWhen());
165
166 rp = factory.create(new R("bob", "1.2.3.4"), db);
167 assertNotNull("have ReceivePack", rp);
168 }
169
170 private static final class R extends HttpServletRequestWrapper {
171 private final String user;
172
173 private final String host;
174
175 R(String user, String host) {
176 super(new Request(null, null) );
177 this.user = user;
178 this.host = host;
179 }
180
181 @Override
182 public String getRemoteHost() {
183 return host;
184 }
185
186 @Override
187 public String getRemoteUser() {
188 return user;
189 }
190 }
191 }