Added AkismetComment class.

This commit is contained in:
Erik C. Thauvin 2019-09-20 06:33:58 -07:00
parent e107f476bd
commit 68144275b2
3 changed files with 319 additions and 450 deletions

View file

@ -47,7 +47,6 @@ import java.time.temporal.ChronoUnit
import java.util.Date
import java.util.logging.Level
import java.util.logging.Logger
import javax.servlet.http.HttpServletRequest
/**
* A small Kotlin/Java library for accessing the Akismet service.
@ -56,26 +55,6 @@ import javax.servlet.http.HttpServletRequest
*/
@Version(properties = "version.properties", type = "kt")
open class Akismet(apiKey: String) {
@Suppress("unused")
companion object {
/** A blog comment. */
const val COMMENT_TYPE_COMMENT = "comment"
/** A top-level forum post. */
const val COMMENT_TYPE_FORUM_POST = "forum-post"
/** A reply to a top-level forum post. */
const val COMMENT_TYPE_REPLY = "reply"
/** A blog post. */
const val COMMENT_TYPE_BLOG_POST = "blog-post"
/** A contact form or feedback form submission. */
const val COMMENT_TYPE_CONTACT_FORM = "contact-form"
/** A new user account. */
const val COMMENT_TYPE_SIGNUP = "signup"
/** A message sent between just a few users. */
const val COMMENT_TYPE_MESSAGE = "message"
/** Administrator role. If used, Akismet will always return false. */
const val ADMIN_ROLE = "administrator"
}
private val apiEndPoint = "https://%srest.akismet.com/1.1/%s"
private val libUserAgent = "${GeneratedVersion.PROJECT}/${GeneratedVersion.VERSION}"
private val verifyMethod = "verify-key"
@ -91,11 +70,6 @@ open class Akismet(apiKey: String) {
field = value
}
/**
* Set the test parameter globally. Can be overwritten in [checkComment], [submitHam] and [submitSpam].
*/
var isTest: Boolean = false
/**
* Check if the API Key has been verified.
*/
@ -171,273 +145,32 @@ open class Akismet(apiKey: String) {
return isVerifiedKey
}
/**
* Comment Check using the content of a
* [HttpServletRequest](https://javaee.github.io/javaee-spec/javadocs/javax/servlet/http/HttpServletRequest.html).
* See the [Akismet API](https://akismet.com/development/api/#comment-check) for more details.
*/
@JvmOverloads
fun checkComment(
request: HttpServletRequest,
permalink: String = "",
type: String = "",
author: String = "",
authorEmail: String = "",
authorUrl: String = "",
content: String = "",
dateGmt: String = "",
postModifiedGmt: String = "",
blogLang: String = "",
blogCharset: String = "",
userRole: String = "",
isTest: Boolean = this.isTest,
recheckReason: String = "",
other: Map<String, String> = emptyMap()
): Boolean {
return checkComment(
userIp = request.remoteAddr,
userAgent = request.getHeader("User-Agent"),
referrer = request.getHeader("Referer"),
permalink = permalink,
type = type,
author = author,
authorEmail = authorEmail,
authorUrl = authorUrl,
content = content,
dateGmt = dateGmt,
postModifiedGmt = postModifiedGmt,
blogLang = blogLang,
blogCharset = blogCharset,
userRole = userRole,
isTest = isTest,
recheckReason = recheckReason,
other = buildPhpVars(request, other))
}
/**
* Comment Check. See the [Akismet API](https://akismet.com/development/api/#comment-check) for more details.
*/
@JvmOverloads
fun checkComment(
userIp: String,
userAgent: String,
referrer: String = "",
permalink: String = "",
type: String = "",
author: String = "",
authorEmail: String = "",
authorUrl: String = "",
content: String = "",
dateGmt: String = "",
postModifiedGmt: String = "",
blogLang: String = "",
blogCharset: String = "",
userRole: String = "",
isTest: Boolean = this.isTest,
recheckReason: String = "",
other: Map<String, String> = emptyMap()
): Boolean {
require(!(userIp.isBlank() && userAgent.isBlank())) { "userIp and/or userAgent are required." }
return executeMethod(
buildApiUrl("comment-check"),
buildFormBody(
userIp = userIp,
userAgent = userAgent,
referrer = referrer,
permalink = permalink,
type = type,
author = author,
authorEmail = authorEmail,
authorUrl = authorUrl,
content = content,
dateGmt = dateGmt,
postModifiedGmt = postModifiedGmt,
blogLang = blogLang,
blogCharset = blogCharset,
userRole = userRole,
isTest = isTest,
recheckReason = recheckReason,
other = other))
}
/**
* Submit Spam (missed spam) using the content of a
* [HttpServletRequest](https://javaee.github.io/javaee-spec/javadocs/javax/servlet/http/HttpServletRequest.html).
* See the [Akismet API](https://akismet.com/development/api/#submit-spam) for more details.
*/
@JvmOverloads
fun submitSpam(
request: HttpServletRequest,
permalink: String = "",
type: String = "",
author: String = "",
authorEmail: String = "",
authorUrl: String = "",
content: String = "",
dateGmt: String = "",
postModifiedGmt: String = "",
blogLang: String = "",
blogCharset: String = "",
userRole: String = "",
isTest: Boolean = this.isTest,
recheckReason: String = "",
other: Map<String, String> = emptyMap()
): Boolean {
return submitSpam(
userIp = request.remoteAddr,
userAgent = request.getHeader("User-Agent"),
referrer = request.getHeader("Referer"),
permalink = permalink,
type = type,
author = author,
authorEmail = authorEmail,
authorUrl = authorUrl,
content = content,
dateGmt = dateGmt,
postModifiedGmt = postModifiedGmt,
blogLang = blogLang,
blogCharset = blogCharset,
userRole = userRole,
isTest = isTest,
recheckReason = recheckReason,
other = buildPhpVars(request, other))
fun checkComment(comment: AkismetComment): Boolean {
return executeMethod(buildApiUrl("comment-check"), buildFormBody(comment))
}
/**
* Submit Spam (missed spam).
* See the [Akismet API](https://akismet.com/development/api/#submit-spam) for more details.
*/
@JvmOverloads
fun submitSpam(
userIp: String,
userAgent: String,
referrer: String = "",
permalink: String = "",
type: String = "",
author: String = "",
authorEmail: String = "",
authorUrl: String = "",
content: String = "",
dateGmt: String = "",
postModifiedGmt: String = "",
blogLang: String = "",
blogCharset: String = "",
userRole: String = "",
isTest: Boolean = this.isTest,
recheckReason: String = "",
other: Map<String, String> = emptyMap()
comment: AkismetComment
): Boolean {
return executeMethod(
buildApiUrl("submit-spam"),
buildFormBody(
userIp = userIp,
userAgent = userAgent,
referrer = referrer,
permalink = permalink,
type = type,
author = author,
authorEmail = authorEmail,
authorUrl = authorUrl,
content = content,
dateGmt = dateGmt,
postModifiedGmt = postModifiedGmt,
blogLang = blogLang,
blogCharset = blogCharset,
userRole = userRole,
isTest = isTest,
recheckReason = recheckReason,
other = other))
}
/**
* Submit Ham (false positives) using the content of a
* [HttpServletRequest](https://javaee.github.io/javaee-spec/javadocs/javax/servlet/http/HttpServletRequest.html).
* See the [Akismet API](https://akismet.com/development/api/#submit-ham) for more details.
*/
@JvmOverloads
fun submitHam(
request: HttpServletRequest,
permalink: String = "",
type: String = "",
author: String = "",
authorEmail: String = "",
authorUrl: String = "",
content: String = "",
dateGmt: String = "",
postModifiedGmt: String = "",
blogLang: String = "",
blogCharset: String = "",
userRole: String = "",
isTest: Boolean = this.isTest,
recheckReason: String = "",
other: Map<String, String> = emptyMap()
): Boolean {
return submitHam(
userIp = request.remoteAddr,
userAgent = request.getHeader("User-Agent"),
referrer = request.getHeader("Referer"),
permalink = permalink,
type = type,
author = author,
authorEmail = authorEmail,
authorUrl = authorUrl,
content = content,
dateGmt = dateGmt,
postModifiedGmt = postModifiedGmt,
blogLang = blogLang,
blogCharset = blogCharset,
userRole = userRole,
isTest = isTest,
recheckReason = recheckReason,
other = buildPhpVars(request, other))
return executeMethod(buildApiUrl("submit-spam"), buildFormBody(comment))
}
/**
* Submit Ham.
* See the [Akismet API](https://akismet.com/development/api/#submit-ham) for more details.
*/
@JvmOverloads
fun submitHam(
userIp: String,
userAgent: String,
referrer: String = "",
permalink: String = "",
type: String = "",
author: String = "",
authorEmail: String = "",
authorUrl: String = "",
content: String = "",
dateGmt: String = "",
postModifiedGmt: String = "",
blogLang: String = "",
blogCharset: String = "",
userRole: String = "",
isTest: Boolean = this.isTest,
recheckReason: String = "",
other: Map<String, String> = emptyMap()
comment: AkismetComment
): Boolean {
return executeMethod(
buildApiUrl("submit-ham"),
buildFormBody(
userIp = userIp,
userAgent = userAgent,
referrer = referrer,
permalink = permalink,
type = type,
author = author,
authorEmail = authorEmail,
authorUrl = authorUrl,
content = content,
dateGmt = dateGmt,
postModifiedGmt = postModifiedGmt,
blogLang = blogLang,
blogCharset = blogCharset,
userRole = userRole,
isTest = isTest,
recheckReason = recheckReason,
other = other))
return executeMethod(buildApiUrl("submit-ham"), buildFormBody(comment))
}
/**
@ -461,7 +194,7 @@ open class Akismet(apiKey: String) {
*
* Execute an Akismet REST API method.
*
* @param apiUrl The Akismet API URL endpoint. (eg. https://rest.akismet.com/1.1/verify-key)
* @param apiUrl The Akismet API URL endpoint. (e.g. https://rest.akismet.com/1.1/verify-key)
* @param formBody The HTTP POST form body containing the request parameters to be submitted.
*/
@Suppress("MemberVisibilityCanBePrivate")
@ -499,94 +232,57 @@ open class Akismet(apiKey: String) {
return String.format(apiEndPoint, "$apiKey.", method).toHttpUrlOrNull()
}
private fun buildPhpVars(request: HttpServletRequest, other: Map<String, String>): HashMap<String, String> {
val params = HashMap<String, String>()
params["REMOTE_ADDR"] = request.remoteAddr
params["REQUEST_URI"] = request.requestURI
val names = request.headerNames
while (names.hasMoreElements()) {
val name = names.nextElement()
if (!name.equals("cookie", true)) {
params["HTTP_${name.toUpperCase()}"] = request.getHeader(name)
}
}
if (other.isNotEmpty()) {
params.putAll(other)
}
return params
}
private fun buildFormBody(
userIp: String,
userAgent: String,
referrer: String,
permalink: String,
type: String,
author: String,
authorEmail: String,
authorUrl: String,
content: String,
dateGmt: String,
postModifiedGmt: String,
blogLang: String,
blogCharset: String,
userRole: String,
isTest: Boolean,
recheckReason: String,
other: Map<String, String>
): FormBody {
private fun buildFormBody(comment: AkismetComment): FormBody {
require(!(comment.userIp.isBlank() && comment.userAgent.isBlank())) { "userIp and/or userAgent are required." }
return FormBody.Builder().apply {
add("blog", blog)
add("user_ip", userIp)
add("user_agent", userAgent)
add("user_ip", comment.userIp)
add("user_agent", comment.userAgent)
if (referrer.isNotBlank()) {
add("referrer", referrer)
if (comment.referrer.isNotBlank()) {
add("referrer", comment.referrer)
}
if (permalink.isNotBlank()) {
add("permalink", permalink)
if (comment.permalink.isNotBlank()) {
add("permalink", comment.permalink)
}
if (type.isNotBlank()) {
add("comment_type", type)
if (comment.type.isNotBlank()) {
add("comment_type", comment.type)
}
if (author.isNotBlank()) {
add("comment_author", author)
if (comment.author.isNotBlank()) {
add("comment_author", comment.author)
}
if (authorEmail.isNotBlank()) {
add("comment_author_email", authorEmail)
if (comment.authorEmail.isNotBlank()) {
add("comment_author_email", comment.authorEmail)
}
if (authorUrl.isNotBlank()) {
add("comment_author_url", authorUrl)
if (comment.authorUrl.isNotBlank()) {
add("comment_author_url", comment.authorUrl)
}
if (content.isNotBlank()) {
add("comment_content", content)
if (comment.content.isNotBlank()) {
add("comment_content", comment.content)
}
if (dateGmt.isNotBlank()) {
add("comment_date_gmt", dateGmt)
if (comment.dateGmt.isNotBlank()) {
add("comment_date_gmt", comment.dateGmt)
}
if (postModifiedGmt.isNotBlank()) {
add("comment_post_modified_gmt", postModifiedGmt)
if (comment.postModifiedGmt.isNotBlank()) {
add("comment_post_modified_gmt", comment.postModifiedGmt)
}
if (blogLang.isNotBlank()) {
add("blog_lang", blogLang)
if (comment.blogLang.isNotBlank()) {
add("blog_lang", comment.blogLang)
}
if (blogCharset.isNotBlank()) {
add("blog_charset", blogCharset)
if (comment.blogCharset.isNotBlank()) {
add("blog_charset", comment.blogCharset)
}
if (userRole.isNotBlank()) {
add("user_role", userRole)
if (comment.userRole.isNotBlank()) {
add("user_role", comment.userRole)
}
if (isTest) {
if (comment.isTest) {
add("is_test", "true")
}
if (recheckReason.isNotBlank()) {
add("recheck_reason", recheckReason)
if (comment.recheckReason.isNotBlank()) {
add("recheck_reason", comment.recheckReason)
}
other.forEach { (k, v) -> add(k, v) }
comment.other.forEach { (k, v) -> add(k, v) }
}.build()
}
}

View file

@ -0,0 +1,235 @@
/*
* AkismetComment.kt
*
* Copyright (c) 2019, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of this project nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.thauvin.erik.akismet
import javax.servlet.http.HttpServletRequest
/**
* A comment to send to Akismet.
*
* @constructor Create an Akismet comment instance. See the
* [Akismet API](https://akismet.com/development/api/#comment-check) for more details.
*/
open class AkismetComment() {
@Suppress("unused")
companion object {
/** A blog comment. */
const val TYPE_COMMENT = "comment"
/** A top-level forum post. */
const val TYPE_FORUM_POST = "forum-post"
/** A reply to a top-level forum post. */
const val TYPE_REPLY = "reply"
/** A blog post. */
const val TYPE_BLOG_POST = "blog-post"
/** A contact form or feedback form submission. */
const val TYPE_CONTACT_FORM = "contact-form"
/** A new user account. */
const val TYPE_SIGNUP = "signup"
/** A message sent between just a few users. */
const val TYPE_MESSAGE = "message"
/** Administrator role. If used, Akismet will always return false. */
const val ADMIN_ROLE = "administrator"
}
/** IP address of the comment submitter. */
var userIp: String = ""
/** User agent string of the web browser submitting the comment. */
var userAgent: String = ""
/** The content of the referer header should be set here. */
var referrer: String = ""
/** The full permanent URL of the entry the comment was submitted to. */
var permalink: String = ""
/**
* A string that describes the type of content being sent, such as [TYPE_COMMENT], [TYPE_FORUM_POST], [TYPE_REPLY],
* [TYPE_BLOG_POST], [TYPE_CONTACT_FORM], [TYPE_SIGNUP], or [TYPE_MESSAGE].
*
* You may send a value not listed above if none of them accurately describe your content.
*
* This is further explained [here](http://blog.akismet.com/2012/06/19/pro-tip-tell-us-your-comment_type/).
*/
var type: String = ""
/** Name submitted with the comment. */
var author: String = ""
/** Email address submitted with the comment. */
var authorEmail: String = ""
/** URL submitted with comment. */
var authorUrl: String = ""
/** The content that was submitted. */
var content: String = ""
/**
* The UTC timestamp of the creation of the comment, in ISO 8601 format.
*
* May be omitted if the comment is sent to the API at the time it is created.
*
* @see [Akismet.dateToGmt]
*/
var dateGmt: String = ""
/** The UTC timestamp of the publication time for the post, page or thread on which the comment was posted. */
var postModifiedGmt: String = ""
/**
* Indicates the language(s) in use on the blog or site, in ISO 639-1 format, comma-separated.
*
* A site with articles in English and French might use: ```en, fr_ca```
*/
var blogLang: String = ""
/**
* The character encoding for the form values included in comment parameters, such as UTF-8 or ISO-8859-1
*/
var blogCharset: String = ""
/**
* The user role of the user who submitted the comment. This is an optional parameter.
*
* If you set it to [ADMIN_ROLE], Akismet will always return false.
*/
var userRole: String = ""
/** This is an optional parameter. You can use it when submitting test queries to Akismet. */
var isTest: Boolean = false
/**
* If you are sending content to Akismet to be rechecked, such as a post that has been edited or old pending
* comments that you'd like to recheck, include this parameter with a string describing why the content is
* being rechecked.
*
* For example: ```edit```
*/
var recheckReason: String = ""
/**
* In PHP, there is an array of environmental variables called $_SERVER that contains information about the Web
* server itself as well as a key/value for every HTTP header sent with the request.
*
* This data is highly useful to Akismet. How the submitted content interacts with the server can be very telling,
* so please include as much of it as possible.
*/
var other: Map<String, String> = emptyMap()
/**
* Create an Akismet comment instance.
* See the [Akismet API](https://akismet.com/development/api/#comment-check) for more details.
*/
constructor(
userIp: String,
userAgent: String,
referrer: String = "",
permalink: String = "",
type: String = "",
author: String = "",
authorEmail: String = "",
authorUrl: String = "",
content: String = "",
dateGmt: String = "",
postModifiedGmt: String = "",
blogLang: String = "",
blogCharset: String = "",
userRole: String = "",
isTest: Boolean = false,
recheckReason: String = "",
other: Map<String, String> = emptyMap()
) : this() {
this.userIp = userIp
this.userAgent = userAgent
this.referrer = referrer
this.permalink = permalink
this.type = type
this.author = author
this.authorEmail = authorEmail
this.authorUrl = authorUrl
this.content = content
this.dateGmt = dateGmt
this.postModifiedGmt = postModifiedGmt
this.blogLang = blogLang
this.blogCharset = blogCharset
this.userRole = userRole
this.isTest = isTest
this.recheckReason = recheckReason
this.other = other
}
/**
* Create Akismet comment extracting [userIp], [userAgent], [referrer] and [other] variables from a
* [HttpServletRequest](https://javaee.github.io/javaee-spec/javadocs/javax/servlet/http/HttpServletRequest.html).
* See the [Akismet API](https://akismet.com/development/api/#comment-check) for more details.
*/
constructor(
request: HttpServletRequest,
permalink: String = "",
type: String = "",
author: String = "",
authorEmail: String = "",
authorUrl: String = "",
content: String = "",
dateGmt: String = "",
postModifiedGmt: String = "",
blogLang: String = "",
blogCharset: String = "",
userRole: String = "",
isTest: Boolean = false,
recheckReason: String = "",
other: Map<String, String> = emptyMap()
) : this(
request.remoteAddr,
request.getHeader("User-Agent"),
request.getHeader("Referer"),
permalink,
type,
author,
authorEmail,
authorUrl,
content,
dateGmt,
postModifiedGmt,
blogLang,
blogCharset,
userRole,
isTest,
recheckReason,
buildPhpVars(request, other))
}
private fun buildPhpVars(request: HttpServletRequest, other: Map<String, String>): HashMap<String, String> {
val params = HashMap<String, String>()
params["REMOTE_ADDR"] = request.remoteAddr
params["REQUEST_URI"] = request.requestURI
val names = request.headerNames
while (names.hasMoreElements()) {
val name = names.nextElement()
if (!name.equals("cookie", true)) {
params["HTTP_${name.toUpperCase()}"] = request.getHeader(name)
}
}
if (other.isNotEmpty()) {
params.putAll(other)
}
return params
}

View file

@ -43,7 +43,6 @@ import java.io.File
import java.io.FileInputStream
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.ZoneOffset
import java.util.Collections
import java.util.Date
import java.util.Properties
@ -75,19 +74,28 @@ fun getKey(key: String): String {
*/
class AkismetTest {
private val blog = getKey("AKISMET_BLOG")
private val userIp = "127.0.0.1"
private val userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6"
private val referrer = "http://www.google.com"
private val permalink = "http://yourblogdomainname.com/blog/post=1"
private val type = Akismet.COMMENT_TYPE_COMMENT
private val author = "admin"
private val authorEmail = "test@test.com"
private val authorUrl = "http://www.CheckOutMyCoolSite.com"
private val content = "It means a lot that you would take the time to review our software. Thanks again."
private val spamAuthor = "viagra-test-123"
private val spamEmail = "akismet-guaranteed-spam@example.com"
private val comment = AkismetComment(
userIp = "127.0.0.1",
userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6",
referrer = "http://www.google.com",
permalink = "http://yourblogdomainname.com/blog/post=1",
type = AkismetComment.TYPE_COMMENT,
author = "admin",
authorEmail = "test@test.com",
authorUrl = "http://www.CheckOutMyCoolSite.com",
userRole = AkismetComment.ADMIN_ROLE,
content = "It means a lot that you would take the time to review our software. Thanks again.",
isTest = true)
private val akismet = Akismet(getKey("AKISMET_API_KEY"), blog)
private val request = Mockito.mock(HttpServletRequest::class.java)
private val mockComment: AkismetComment = AkismetComment(
request = getMockRequest(),
permalink = comment.permalink,
type = comment.type,
author = comment.author,
authorUrl = comment.authorUrl,
userRole = AkismetComment.ADMIN_ROLE,
content = comment.content,
isTest = true)
@BeforeClass
fun beforeClass() {
@ -95,15 +103,6 @@ class AkismetTest {
addHandler(ConsoleHandler().apply { level = Level.FINE })
level = Level.FINE
}
Mockito.`when`(request.remoteAddr).thenReturn(userIp)
Mockito.`when`(request.requestURI).thenReturn("/blog/post=1")
Mockito.`when`(request.getHeader("User-Agent")).thenReturn(userAgent)
Mockito.`when`(request.getHeader("Referer")).thenReturn(referrer)
Mockito.`when`(request.getHeader("Cookie")).thenReturn("name=value; name2=value2; name3=value3")
Mockito.`when`(request.getHeader("Accept-Encoding")).thenReturn("gzip")
Mockito.`when`(request.headerNames)
.thenReturn(Collections.enumeration(listOf("User-Agent", "Referer", "Cookie", "Accept-Encoding")))
}
@Test
@ -132,97 +131,27 @@ class AkismetTest {
@Test
fun checkCommentTest() {
assertFalse(
akismet.checkComment(
userIp = userIp,
userAgent = userAgent,
referrer = referrer,
permalink = permalink,
type = type,
author = author,
authorEmail = authorEmail,
authorUrl = authorUrl,
content = content,
userRole = Akismet.ADMIN_ROLE,
isTest = true), "check_comment(admin) -> false")
assertFalse(akismet.checkComment(comment), "check_comment(admin) -> false")
comment.userRole = ""
assertTrue(akismet.checkComment(comment), "check_comment -> true")
akismet.isTest = true
assertTrue(
akismet.checkComment(
userIp = userIp,
userAgent = userAgent,
referrer = referrer,
permalink = permalink,
type = type,
author = spamAuthor,
authorEmail = spamEmail,
authorUrl = authorUrl,
content = content), "check_comment -> true")
assertTrue(
akismet.checkComment(
request,
permalink = permalink,
type = type,
author = spamAuthor,
authorEmail = spamEmail,
authorUrl = authorUrl,
content = content), "check_comment(request) -> true")
assertFalse(akismet.checkComment(mockComment), "check_comment(request) -> false")
mockComment.userRole = ""
assertTrue(akismet.checkComment(mockComment), "check_comment(request) -> true")
}
@Test
fun submitHamTest() {
assertTrue(
akismet.submitHam(
userIp = userIp,
userAgent = userAgent,
referrer = referrer,
permalink = permalink,
type = type,
author = author,
authorEmail = authorEmail,
authorUrl = authorUrl,
content = content,
isTest = true), "submitHam")
assertTrue(akismet.submitHam(comment), "submitHam")
assertTrue(
akismet.submitHam(
request,
permalink = permalink,
type = type,
author = author,
authorEmail = authorEmail,
authorUrl = authorUrl,
content = content,
isTest = true), "submitHam(request)")
assertTrue(akismet.submitHam(mockComment), "submitHam(request)")
}
@Test
fun submitSpamTest() {
assertTrue(
akismet.submitSpam(
userIp = userIp,
userAgent = userAgent,
referrer = referrer,
permalink = permalink,
type = type,
author = spamAuthor,
authorEmail = spamEmail,
authorUrl = authorUrl,
content = content,
isTest = true), "submitHam")
assertTrue(akismet.submitSpam(comment), "submitHam")
assertTrue(
akismet.submitSpam(
request,
permalink = permalink,
type = type,
author = spamAuthor,
authorEmail = spamEmail,
authorUrl = authorUrl,
content = content,
isTest = true), "submitHam(request)")
assertTrue(akismet.submitSpam(mockComment), "submitHam(request)")
}
@Test
@ -233,9 +162,18 @@ class AkismetTest {
akismet.dateToGmt(date),
akismet.dateToGmt(localDateTime),
"dateGmt(date) == dateGmt(localDateTime)")
assertEquals(
localDateTime.atOffset(ZoneOffset.UTC).toEpochSecond(),
akismet.dateToGmt(date).toLong(),
"localDateTime = dateGmt")
}
private fun getMockRequest(): HttpServletRequest {
val request = Mockito.mock(HttpServletRequest::class.java)
Mockito.`when`(request.remoteAddr).thenReturn(comment.userIp)
Mockito.`when`(request.requestURI).thenReturn("/blog/post=1")
Mockito.`when`(request.getHeader("User-Agent")).thenReturn(comment.userAgent)
Mockito.`when`(request.getHeader("Referer")).thenReturn(comment.referrer)
Mockito.`when`(request.getHeader("Cookie")).thenReturn("name=value; name2=value2; name3=value3")
Mockito.`when`(request.getHeader("Accept-Encoding")).thenReturn("gzip")
Mockito.`when`(request.headerNames)
.thenReturn(Collections.enumeration(listOf("User-Agent", "Referer", "Cookie", "Accept-Encoding")))
return request
}
}