Updated tests.
This commit is contained in:
parent
0cdddcfd2c
commit
72ae537e48
1 changed files with 71 additions and 15 deletions
|
@ -32,6 +32,8 @@
|
|||
|
||||
package net.thauvin.erik.akismet
|
||||
|
||||
import okhttp3.FormBody
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
||||
import org.mockito.Mockito
|
||||
import org.testng.Assert.assertEquals
|
||||
import org.testng.Assert.assertFalse
|
||||
|
@ -73,11 +75,15 @@ fun getKey(key: String): String {
|
|||
* AKISMET_API_KEY and AKISMET_BLOG should be in env vars or local.properties
|
||||
*/
|
||||
class AkismetTest {
|
||||
private val apiKey = getKey("AKISMET_API_KEY")
|
||||
private val blog = getKey("AKISMET_BLOG")
|
||||
private val referer = "http://www.google.com"
|
||||
private val date = Date()
|
||||
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")
|
||||
private val akismet = Akismet(getKey("AKISMET_API_KEY"), blog)
|
||||
userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6"
|
||||
)
|
||||
private val akismet = Akismet(apiKey, blog)
|
||||
private val mockComment: AkismetComment = AkismetComment(request = getMockRequest())
|
||||
|
||||
@BeforeClass
|
||||
|
@ -87,32 +93,56 @@ class AkismetTest {
|
|||
level = Level.FINE
|
||||
}
|
||||
|
||||
comment.referrer = "http://www.google.com"
|
||||
comment.referrer = referer
|
||||
comment.permalink = "http://yourblogdomainname.com/blog/post=1"
|
||||
comment.type = AkismetComment.TYPE_COMMENT
|
||||
comment.author = "admin"
|
||||
comment.authorEmail = "test@test.com"
|
||||
comment.authorUrl = "http://www.CheckOutMyCoolSite.com"
|
||||
comment.userRole = AkismetComment.ADMIN_ROLE
|
||||
comment.content = "It means a lot that you would take the time to review our software. Thanks again."
|
||||
comment.dateGmt = akismet.dateToGmt(date)
|
||||
comment.postModifiedGmt = comment.dateGmt
|
||||
comment.blogLang = "en"
|
||||
comment.blogCharset = "UTF-8"
|
||||
comment.userRole = AkismetComment.ADMIN_ROLE
|
||||
comment.isTest = true
|
||||
|
||||
akismet.logger.info(comment.toString())
|
||||
|
||||
mockComment.permalink = comment.permalink
|
||||
mockComment.type = comment.type
|
||||
mockComment.authorEmail = comment.authorEmail
|
||||
mockComment.author = comment.author
|
||||
mockComment.authorUrl = comment.authorUrl
|
||||
mockComment.userRole = AkismetComment.ADMIN_ROLE
|
||||
mockComment.content = comment.content
|
||||
mockComment.dateGmt = comment.dateGmt
|
||||
mockComment.postModifiedGmt = comment.dateGmt
|
||||
mockComment.blogLang = comment.blogLang
|
||||
mockComment.blogCharset = comment.blogCharset
|
||||
mockComment.userRole = comment.userRole
|
||||
mockComment.recheckReason = "edit"
|
||||
mockComment.isTest = true
|
||||
|
||||
akismet.logger.info(mockComment.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun constructorsTest() {
|
||||
expectThrows(IllegalArgumentException::class.java) {
|
||||
Akismet("123456789012", "http://www.foo.com/")
|
||||
Akismet("", "http://www.foo.com/")
|
||||
Akismet("")
|
||||
}
|
||||
expectThrows(IllegalArgumentException::class.java) {
|
||||
Akismet("1234")
|
||||
}
|
||||
expectThrows(IllegalArgumentException::class.java) {
|
||||
Akismet("123456789 12")
|
||||
}
|
||||
expectThrows(IllegalArgumentException::class.java) {
|
||||
Akismet("123456789012", "")
|
||||
}
|
||||
expectThrows(IllegalArgumentException::class.java) {
|
||||
Akismet("1234", "foo")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -130,16 +160,34 @@ class AkismetTest {
|
|||
assertEquals(akismet.response, "valid", "response -> valid")
|
||||
assertTrue(akismet.isVerifiedKey, "isVerifiedKey -> true")
|
||||
|
||||
assertFalse(Akismet("123456789012").verifyKey(), "verifyKey() --> false")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun checkMockComment() {
|
||||
assertEquals(mockComment.userIp, comment.userIp, "userIp")
|
||||
assertEquals(mockComment.userAgent, comment.userAgent, "userAgent")
|
||||
assertEquals(mockComment.referrer, comment.referrer, "referrer")
|
||||
assertTrue(mockComment.serverEnv.containsKey("HTTP_ACCEPT_ENCODING"), "HTTP_ACCEPT_ENCODING")
|
||||
assertTrue(mockComment.serverEnv.containsKey("REQUEST_URI"), "REQUEST_URI")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun emptyCommentTest() {
|
||||
expectThrows(IllegalArgumentException::class.java) {
|
||||
akismet.checkComment(AkismetComment("", ""))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun resetTest() {
|
||||
akismet.reset()
|
||||
assertTrue(
|
||||
akismet.debugHelp == "" && akismet.httpStatusCode == 0 && !akismet.isDiscard &&
|
||||
!akismet.isVerifiedKey && akismet.proTip == "" && akismet.response == ""
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun checkCommentTest() {
|
||||
assertFalse(akismet.checkComment(comment), "check_comment(admin) -> false")
|
||||
|
@ -151,6 +199,17 @@ class AkismetTest {
|
|||
assertFalse(akismet.checkComment(mockComment), "check_comment(request) -> false")
|
||||
mockComment.userRole = ""
|
||||
assertTrue(akismet.checkComment(mockComment), "check_comment(request) -> true")
|
||||
|
||||
assertEquals(akismet.httpStatusCode, 200, "status code")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun executeMethodTest() {
|
||||
akismet.executeMethod(
|
||||
"https://$apiKey.rest.akismet.com/1.1/comment-check".toHttpUrlOrNull(),
|
||||
FormBody.Builder().apply { add("is_test", "1") }.build()
|
||||
)
|
||||
assertTrue(akismet.debugHelp.isNotEmpty(), "debugHelp not empty")
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -163,18 +222,15 @@ class AkismetTest {
|
|||
@Test
|
||||
fun submitSpamTest() {
|
||||
assertTrue(akismet.submitSpam(comment), "submitHam")
|
||||
|
||||
assertTrue(akismet.submitSpam(mockComment), "submitHam(request)")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun dateToGmtTest() {
|
||||
val date = Date()
|
||||
val localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault())
|
||||
assertEquals(
|
||||
akismet.dateToGmt(date),
|
||||
akismet.dateToGmt(localDateTime),
|
||||
"dateGmt(date) == dateGmt(localDateTime)")
|
||||
val utcDate = akismet.dateToGmt(date)
|
||||
assertEquals(akismet.dateToGmt(localDateTime), utcDate, "dateGmt(localDateTime) = utcDate")
|
||||
assertEquals(comment.dateGmt, utcDate, "dateGmt == utcDate")
|
||||
}
|
||||
|
||||
private fun getMockRequest(): HttpServletRequest {
|
||||
|
@ -182,11 +238,11 @@ class AkismetTest {
|
|||
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("referer")).thenReturn(referer)
|
||||
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")))
|
||||
.thenReturn(Collections.enumeration(listOf("User-Agent", "referer", "Cookie", "Accept-Encoding")))
|
||||
return request
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue