1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.http.test;
12
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertSame;
15 import static org.junit.Assert.fail;
16
17 import java.io.IOException;
18
19 import javax.servlet.http.HttpServletRequest;
20 import javax.servlet.http.HttpServletRequestWrapper;
21
22 import org.eclipse.jetty.server.Request;
23 import org.eclipse.jgit.http.server.resolver.DefaultUploadPackFactory;
24 import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
25 import org.eclipse.jgit.lib.Repository;
26 import org.eclipse.jgit.lib.StoredConfig;
27 import org.eclipse.jgit.transport.UploadPack;
28 import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
29 import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
30 import org.eclipse.jgit.transport.resolver.UploadPackFactory;
31 import org.junit.Before;
32 import org.junit.Test;
33
34 public class DefaultUploadPackFactoryTest extends LocalDiskRepositoryTestCase {
35 private Repository db;
36
37 private UploadPackFactory<HttpServletRequest> factory;
38
39 @Override
40 @Before
41 public void setUp() throws Exception {
42 super.setUp();
43
44 db = createBareRepository();
45 factory = new DefaultUploadPackFactory();
46 }
47
48 @SuppressWarnings("unchecked")
49 @Test
50 public void testDisabledSingleton() throws ServiceNotAuthorizedException {
51 factory = (UploadPackFactory<HttpServletRequest>) UploadPackFactory.DISABLED;
52
53 try {
54 factory.create(new R(null, "localhost"), db);
55 fail("Created session for anonymous user: null");
56 } catch (ServiceNotEnabledException e) {
57
58 }
59
60 try {
61 factory.create(new R("", "localhost"), db);
62 fail("Created session for anonymous user: \"\"");
63 } catch (ServiceNotEnabledException e) {
64
65 }
66
67 try {
68 factory.create(new R("bob", "localhost"), db);
69 fail("Created session for user: \"bob\"");
70 } catch (ServiceNotEnabledException e) {
71
72 }
73 }
74
75 @Test
76 public void testCreate_Default() throws ServiceNotEnabledException,
77 ServiceNotAuthorizedException {
78 UploadPack up;
79
80 up = factory.create(new R(null, "1.2.3.4"), db);
81 assertNotNull("have UploadPack", up);
82 assertSame(db, up.getRepository());
83
84 up = factory.create(new R("bob", "1.2.3.4"), db);
85 assertNotNull("have UploadPack", up);
86 assertSame(db, up.getRepository());
87 }
88
89 @Test
90 public void testCreate_Disabled() throws ServiceNotAuthorizedException,
91 IOException {
92 final StoredConfig cfg = db.getConfig();
93 cfg.setBoolean("http", null, "uploadpack", false);
94 cfg.save();
95
96 try {
97 factory.create(new R(null, "localhost"), db);
98 fail("Created session for anonymous user: null");
99 } catch (ServiceNotEnabledException e) {
100
101 }
102
103 try {
104 factory.create(new R("bob", "localhost"), db);
105 fail("Created session for user: \"bob\"");
106 } catch (ServiceNotEnabledException e) {
107
108 }
109 }
110
111 @Test
112 public void testCreate_Enabled() throws ServiceNotEnabledException,
113 ServiceNotAuthorizedException {
114 db.getConfig().setBoolean("http", null, "uploadpack", true);
115 UploadPack up;
116
117 up = factory.create(new R(null, "1.2.3.4"), db);
118 assertNotNull("have UploadPack", up);
119 assertSame(db, up.getRepository());
120
121 up = factory.create(new R("bob", "1.2.3.4"), db);
122 assertNotNull("have UploadPack", up);
123 assertSame(db, up.getRepository());
124 }
125
126 private static final class R extends HttpServletRequestWrapper {
127 private final String user;
128
129 private final String host;
130
131 R(String user, String host) {
132 super(new Request(null, null) );
133 this.user = user;
134 this.host = host;
135 }
136
137 @Override
138 public String getRemoteHost() {
139 return host;
140 }
141
142 @Override
143 public String getRemoteUser() {
144 return user;
145 }
146 }
147 }