2023-01-12 04:42:23 +04:00
|
|
|
package inmemdb_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-03-05 00:18:52 +04:00
|
|
|
"git.arav.su/Arav/justcaptcha/pkg/captcha"
|
|
|
|
"git.arav.su/Arav/justcaptcha/pkg/captcha/inmemdb"
|
|
|
|
"git.arav.su/Arav/justcaptcha/pkg/dwcaptcha"
|
2023-01-12 04:42:23 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
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) {
|
2023-03-05 00:30:24 +04:00
|
|
|
db := inmemdb.NewInMemoryDB(expiry)
|
2023-01-12 04:42:23 +04:00
|
|
|
|
|
|
|
if db.GetExpiry() != expiry {
|
|
|
|
t.Errorf("expected expiry %v, but got %v", expiry, db.GetExpiry())
|
|
|
|
}
|
|
|
|
}
|