Commit a2965e23 authored by Liang Ding's avatar Liang Ding

增加后台单元测试

parent 7f89e4ba
......@@ -310,7 +310,8 @@ public final class SoloServletListener extends AbstractServletListener {
/**
* 部分控制器使用函数式路由. https://github.com/b3log/solo/issues/12580
*/
private void routePartial() {
public static void routePartial() {
final BeanManager beanManager = BeanManager.getInstance();
final AdminConsole adminConsole = beanManager.getReference(AdminConsole.class);
DispatcherServlet.get("/admin-index.do", adminConsole::showAdminIndex);
DispatcherServlet.get("/admin-preference.do", adminConsole::showAdminPreferenceFunction);
......
......@@ -17,23 +17,29 @@
*/
package org.b3log.solo;
import org.apache.commons.lang.RandomStringUtils;
import org.b3log.latke.Keys;
import org.b3log.latke.Latkes;
import org.b3log.latke.ioc.BeanManager;
import org.b3log.latke.ioc.Discoverer;
import org.b3log.latke.model.User;
import org.b3log.latke.repository.jdbc.util.Connections;
import org.b3log.latke.repository.jdbc.util.JdbcRepositories;
import org.b3log.latke.service.ServiceException;
import org.b3log.latke.util.Crypts;
import org.b3log.solo.api.MetaWeblogAPI;
import org.b3log.solo.cache.*;
import org.b3log.solo.processor.MockDispatcherServlet;
import org.b3log.solo.repository.*;
import org.b3log.solo.service.*;
import org.b3log.solo.util.Solos;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.io.StringWriter;
......@@ -124,6 +130,25 @@ public abstract class AbstractTestCase {
Assert.assertNotNull(userQueryService.getUserByEmailOrUserName("test@gmail.com"));
}
/**
* Mocks admin login for console testing.
*
* @param request the specified request
* @throws ServiceException service exception
*/
public void mockAdminLogin(final MockHttpServletRequest request) throws ServiceException {
final JSONObject adminUser = getUserQueryService().getAdmin();
final String userId = adminUser.optString(Keys.OBJECT_ID);
final JSONObject cookieJSONObject = new JSONObject();
cookieJSONObject.put(Keys.OBJECT_ID, userId);
cookieJSONObject.put(User.USER_PASSWORD, adminUser.optString(User.USER_PASSWORD));
final String random = RandomStringUtils.randomAlphanumeric(16);
cookieJSONObject.put(Keys.TOKEN, adminUser.optString(User.USER_PASSWORD) + ":" + random);
final String cookieValue = Crypts.encryptByAES(cookieJSONObject.toString(), Solos.COOKIE_SECRET);
final Cookie cookie = new Cookie(Solos.COOKIE_NAME, cookieValue);
request.setCookies(new Cookie[]{cookie});
}
/**
* Gets a mock dispatcher servlet and run service.
*
......@@ -134,6 +159,7 @@ public abstract class AbstractTestCase {
public MockDispatcherServlet mockDispatcherServletService(final HttpServletRequest request, final MockHttpServletResponse response) {
final MockDispatcherServlet ret = new MockDispatcherServlet();
ret.init();
SoloServletListener.routePartial();
ret.service(request, response);
return ret;
......
/*
* Solo - A small and beautiful blogging system written in Java.
* Copyright (c) 2010-2018, b3log.org & hacpai.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.b3log.solo.processor.console;
import org.apache.commons.lang.StringUtils;
import org.b3log.solo.AbstractTestCase;
import org.b3log.solo.MockHttpServletRequest;
import org.b3log.solo.MockHttpServletResponse;
import org.testng.Assert;
import org.testng.annotations.Test;
/**
* {@link AdminConsole} test case.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.1, Dec 7, 2018
* @since 2.9.7
*/
@Test(suiteName = "processor")
public class AdminConsoleTestCase extends AbstractTestCase {
/**
* Init.
*
* @throws Exception exception
*/
@Test
public void init() throws Exception {
super.init();
}
/**
* showAdminIndex.
*
* @throws Exception exception
*/
public void showAdminIndex() throws Exception {
final MockHttpServletRequest request = mockRequest();
request.setRequestURI("/admin-index.do");
mockAdminLogin(request);
final MockHttpServletResponse response = mockResponse();
mockDispatcherServletService(request, response);
final String content = response.body();
Assert.assertTrue(StringUtils.contains(content, "Admin 的个人博客 - 后台管理"));
}
}
......@@ -115,17 +115,4 @@ public class CategoryConsoleTestCase extends AbstractTestCase {
Assert.assertNotNull(category);
Assert.assertEquals(category.optInt(Category.CATEGORY_TAG_CNT), 1); // https://github.com/b3log/solo/issues/12274
}
private void mockAdminLogin(final MockHttpServletRequest request) throws ServiceException {
final JSONObject adminUser = getUserQueryService().getAdmin();
final String userId = adminUser.optString(Keys.OBJECT_ID);
final JSONObject cookieJSONObject = new JSONObject();
cookieJSONObject.put(Keys.OBJECT_ID, userId);
cookieJSONObject.put(User.USER_PASSWORD, adminUser.optString(User.USER_PASSWORD));
final String random = RandomStringUtils.randomAlphanumeric(16);
cookieJSONObject.put(Keys.TOKEN, adminUser.optString(User.USER_PASSWORD) + ":" + random);
final String cookieValue = Crypts.encryptByAES(cookieJSONObject.toString(), Solos.COOKIE_SECRET);
final Cookie cookie = new Cookie(Solos.COOKIE_NAME, cookieValue);
request.setCookies(new Cookie[]{cookie});
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment