Debian Codegraph @

tests/test-loginservice/src/test/java/org/eclipse/jetty/JdbcLoginServiceTest.java

raw
1 //
2 // ========================================================================
3 // Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
8 //
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
11 //
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
14 //
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
17 //
18
19 package org.eclipse.jetty;
20
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.FileReader;
25 import java.io.PrintWriter;
26 import java.net.URI;
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.eclipse.jetty.client.HttpClient;
30 import org.eclipse.jetty.client.api.AuthenticationStore;
31 import org.eclipse.jetty.client.api.ContentResponse;
32 import org.eclipse.jetty.client.api.Request;
33 import org.eclipse.jetty.client.util.BasicAuthentication;
34 import org.eclipse.jetty.client.util.BytesContentProvider;
35 import org.eclipse.jetty.http.HttpMethod;
36 import org.eclipse.jetty.http.HttpStatus;
37 import org.eclipse.jetty.security.JDBCLoginService;
38 import org.eclipse.jetty.security.LoginService;
39 import org.eclipse.jetty.toolchain.test.FS;
40 import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
41 import org.eclipse.jetty.util.IO;
42 import org.junit.jupiter.api.AfterAll;
43 import org.junit.jupiter.api.AfterEach;
44 import org.junit.jupiter.api.BeforeAll;
45 import org.junit.jupiter.api.BeforeEach;
46 import org.junit.jupiter.api.Test;
47 import org.testcontainers.junit.jupiter.Testcontainers;
48
49 import static org.junit.jupiter.api.Assertions.assertEquals;
50 import static org.junit.jupiter.api.Assertions.assertTrue;
51
52 @Testcontainers(disabledWithoutDocker = true)
53 public class JdbcLoginServiceTest
54 {
55 private static String _content =
56 "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
57
58 private static File __docRoot;
59 private static String __realm = "JdbcRealm";
60 private static URI __baseUri;
61 private static DatabaseLoginServiceTestServer __testServer;
62 private HttpClient _client;
63 private AuthenticationStore _authStore;
64
65 @BeforeAll
66 public static void setUp() throws Exception
67 {
68 File dir = MavenTestingUtils.getTargetTestingDir("jdbcloginservice-test");
69 FS.ensureDirExists(dir);
70
71 //create the realm properties file based on dynamic + static info
72 File skeletonFile = MavenTestingUtils.getTestResourceFile("jdbcrealm.properties");
73 File realmFile = new File(dir, "realm.properties");
74 try (PrintWriter writer = new PrintWriter(new FileOutputStream(realmFile)))
75 {
76 writer.println("jdbcdriver = " + DatabaseLoginServiceTestServer.MARIA_DB_DRIVER_CLASS);
77 writer.println("url = " + DatabaseLoginServiceTestServer.MARIA_DB_URL);
78 writer.println("username = " + DatabaseLoginServiceTestServer.MARIA_DB_USER);
79 writer.println("password = " + DatabaseLoginServiceTestServer.MARIA_DB_PASSWORD);
80 IO.copy(new FileReader(skeletonFile), writer);
81 }
82
83 //make some static content
84 __docRoot = new File(dir, "docroot");
85 FS.ensureDirExists(__docRoot);
86 File content = new File(__docRoot, "input.txt");
87 try (FileOutputStream out = new FileOutputStream(content))
88 {
89 out.write(_content.getBytes("utf-8"));
90 }
91
92 LoginService loginService = new JDBCLoginService(__realm, realmFile.getAbsolutePath());
93
94 __testServer = new DatabaseLoginServiceTestServer();
95 __testServer.setResourceBase(__docRoot.getAbsolutePath());
96 __testServer.setLoginService(loginService);
97 __testServer.start();
98
99 __baseUri = __testServer.getBaseUri();
100 }
101
102 @AfterAll
103 public static void tearDown()
104 throws Exception
105 {
106 if (__testServer != null)
107 {
108 __testServer.stop();
109 __testServer = null;
110 }
111 }
112
113 @BeforeEach
114 public void setupClient() throws Exception
115 {
116 _client = new HttpClient();
117 _authStore = _client.getAuthenticationStore();
118 }
119
120 @AfterEach
121 public void stopClient() throws Exception
122 {
123 if (_client != null)
124 {
125 _client.stop();
126 _client = null;
127 }
128 }
129
130 @Test
131 public void testPut() throws Exception
132 {
133 _authStore.addAuthentication(new BasicAuthentication(__baseUri, __realm, "jetty", "jetty"));
134 _client.start();
135
136 Request request = _client.newRequest(__baseUri.resolve("output.txt"));
137 request.method(HttpMethod.PUT);
138 request.content(new BytesContentProvider(_content.getBytes()));
139 ContentResponse response = request.send();
140 int responseStatus = response.getStatus();
141 boolean statusOk = (responseStatus == 200 || responseStatus == 201);
142 assertTrue(statusOk);
143 String content = IO.toString(new FileInputStream(new File(__docRoot, "output.txt")));
144 assertEquals(_content, content);
145 }
146
147 @Test
148 public void testGet() throws Exception
149 {
150 _authStore.addAuthentication(new BasicAuthentication(__baseUri, __realm, "jetty", "jetty"));
151 _client.start();
152
153 ContentResponse response = _client.GET(__baseUri.resolve("input.txt"));
154 assertEquals(HttpServletResponse.SC_OK, response.getStatus());
155 assertEquals(_content, response.getContentAsString());
156 }
157
158 @Test
159 public void testGetNonExistantUser() throws Exception
160 {
161 _authStore.addAuthentication(new BasicAuthentication(__baseUri, __realm, "foo", "bar"));
162 _client.start();
163
164 ContentResponse response = _client.GET(__baseUri.resolve("input.txt"));
165 assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getStatus());
166 }
167
168 @Test
169 public void testHead() throws Exception
170 {
171 _authStore.addAuthentication(new BasicAuthentication(__baseUri, __realm, "jetty", "jetty"));
172 _client.start();
173
174 Request request = _client.newRequest(__baseUri.resolve("input.txt"));
175 request.method(HttpMethod.HEAD);
176 ContentResponse response = request.send();
177 int responseStatus = response.getStatus();
178 assertEquals(HttpStatus.OK_200, responseStatus);
179 }
180
181 @Test
182 public void testPost() throws Exception
183 {
184 _authStore.addAuthentication(new BasicAuthentication(__baseUri, __realm, "jetty", "jetty"));
185 _client.start();
186
187 Request request = _client.newRequest(__baseUri.resolve("test"));
188 request.method(HttpMethod.POST);
189 request.content(new BytesContentProvider(_content.getBytes()));
190 ContentResponse response = request.send();
191 assertEquals(HttpStatus.OK_200, response.getStatus());
192 assertEquals(_content, __testServer.getTestHandler().getRequestContent());
193
194 }
195 }