Added tests for InMemoryCaptchaDB and DwellingCaptcha.

This commit is contained in:
Alexander Andreev 2023-01-12 04:42:23 +04:00
parent e26bdd64f5
commit 4f254db2bb
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F
2 changed files with 78 additions and 0 deletions

View File

@ -0,0 +1,48 @@
package inmemdb_test
import (
"testing"
"time"
"git.arav.top/Arav/justcaptcha/pkg/captcha"
"git.arav.top/Arav/justcaptcha/pkg/captcha/inmemdb"
"git.arav.top/Arav/justcaptcha/pkg/dwcaptcha"
)
const expiry = 10 * time.Minute
const testData = "192.168.0.1"
func TestInMemDBDefaultInstance(t *testing.T) {
if inmemdb.GetExpiry() != captcha.DefaultExpiredScanInterval {
t.Errorf("expiration is different from a default one (%v != %v)",
inmemdb.GetExpiry(), captcha.DefaultExpiredScanInterval)
}
inmemdb.SetExpiry(expiry)
if expiry != inmemdb.GetExpiry() {
t.Errorf("Expected %v, but got %v", expiry, inmemdb.GetExpiry())
}
captcha, captchaID := inmemdb.New(testData, dwcaptcha.NewDwellingCaptcha(expiry))
if inmemdb.Image(captchaID, "") == nil {
t.Error("nil returned instead of an image.Image struct")
}
if !inmemdb.Solve(captchaID, captcha.Answer()) {
t.Error("CAPTCHA not solved. Looks like provided ID wasn't found")
}
if !inmemdb.IsSolved(captchaID) {
t.Error("last test solved a CAPTCHA, but IsSolved() returned false")
}
}
func TestInMemDBNewInstance(t *testing.T) {
db := inmemdb.NewInMemoryCaptchaDB(expiry)
if db.GetExpiry() != expiry {
t.Errorf("expected expiry %v, but got %v", expiry, db.GetExpiry())
}
}

View File

@ -0,0 +1,30 @@
package dwcaptcha_test
import (
"testing"
"time"
"git.arav.top/Arav/justcaptcha/pkg/dwcaptcha"
)
const expiry = 10 * time.Minute
func TestCaptcha(t *testing.T) {
captcha := dwcaptcha.NewDwellingCaptcha(expiry)
if diff := time.Until(captcha.Expiry()) - expiry; diff >= 1*time.Microsecond {
t.Errorf("Difference %s is more than 1 microsecond", diff.String())
}
if captcha.Image("") == nil {
t.Error("image cannot be nil, but it is!")
}
if !captcha.Solve(captcha.Answer()) {
t.Error("for whatever reason, its own answer doesn't match itself O_O")
}
if !captcha.IsSolved() {
t.Error("after last test it should be true, but it is not!")
}
}