Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
solo-1
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
solo-1
Commits
a2965e23
Unverified
Commit
a2965e23
authored
Dec 07, 2018
by
Liang Ding
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
✅
增加后台单元测试
parent
7f89e4ba
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
92 additions
and
14 deletions
+92
-14
src/main/java/org/b3log/solo/SoloServletListener.java
src/main/java/org/b3log/solo/SoloServletListener.java
+2
-1
src/test/java/org/b3log/solo/AbstractTestCase.java
src/test/java/org/b3log/solo/AbstractTestCase.java
+26
-0
src/test/java/org/b3log/solo/processor/console/AdminConsoleTestCase.java
...rg/b3log/solo/processor/console/AdminConsoleTestCase.java
+64
-0
src/test/java/org/b3log/solo/processor/console/CategoryConsoleTestCase.java
...b3log/solo/processor/console/CategoryConsoleTestCase.java
+0
-13
No files found.
src/main/java/org/b3log/solo/SoloServletListener.java
View file @
a2965e23
...
...
@@ -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
);
...
...
src/test/java/org/b3log/solo/AbstractTestCase.java
View file @
a2965e23
...
...
@@ -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
;
...
...
src/test/java/org/b3log/solo/processor/console/AdminConsoleTestCase.java
0 → 100644
View file @
a2965e23
/*
* 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 的个人博客 - 后台管理"
));
}
}
src/test/java/org/b3log/solo/processor/console/CategoryConsoleTestCase.java
View file @
a2965e23
...
...
@@ -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
});
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment