-
Notifications
You must be signed in to change notification settings - Fork 0
/
captcha_answer_test.go
98 lines (80 loc) · 2.29 KB
/
captcha_answer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package captchasolve
import (
"testing"
"time"
captchatoolsgo "github.com/Matthew17-21/Captcha-Tools/captchatools-go"
"github.com/stretchr/testify/require"
)
func TestIsExpired(t *testing.T) {
tests := []struct {
Id string
SolvedAt time.Time
Expected bool
}{
{Id: "1", SolvedAt: time.Now(), Expected: false},
{Id: "2", SolvedAt: time.Now().Add(time.Minute), Expected: false},
{Id: "3", SolvedAt: time.Now().Add(time.Hour), Expected: false},
{Id: "4", SolvedAt: time.Now().Add(-time.Minute), Expected: false},
{Id: "5", SolvedAt: time.Now().Add(-captchaTokenValidity), Expected: true},
{Id: "6", SolvedAt: time.Now().Add(-time.Hour), Expected: true},
}
for _, tt := range tests {
t.Run(tt.Id, func(t *testing.T) {
// Create captcha answer
ca := CaptchaAnswer{solvedAt: tt.SolvedAt}
// Run function
result := ca.IsExpired()
// Assert
require.Equal(t, tt.Expected, result)
})
}
}
func TestToCaptchaAnswer(t *testing.T) {
// Setup
input := &captchatoolsgo.CaptchaAnswer{
Token: "token",
UserAgent: "ua",
}
// Test execution
result := toCaptchaAnswer(input)
// Assertions
require.NotNil(t, result)
require.Equal(t, input.Token, result.Token)
require.Equal(t, input.UserAgent, result.UserAgent)
// Verify that solvedAt time is recent
timeDiff := time.Since(result.solvedAt)
require.Less(t, timeDiff, 2*time.Second)
}
func TestNewCaptchaAnswer(t *testing.T) {
// Setup
input := &captchatoolsgo.CaptchaAnswer{
Token: "token",
UserAgent: "ua",
}
fixedTime := time.Now()
// Test execution
result := newCaptchaAnswer(input, fixedTime)
// Assertions
require.NotNil(t, result)
require.Equal(t, input.Token, result.Token)
require.Equal(t, input.UserAgent, result.UserAgent)
require.Equal(t, fixedTime, result.solvedAt)
}
func TestToCaptchaAnswerWithNilInput(t *testing.T) {
// Test execution
result := toCaptchaAnswer(nil)
// Assertions
require.NotNil(t, result)
require.Empty(t, result.Id())
require.Empty(t, result.Token)
require.True(t, result.solvedAt.IsZero())
}
func TestNewCaptchaAnswerWithNilInput(t *testing.T) {
// Setup & test execution
result := newCaptchaAnswer(nil, time.Now())
// Assertions
require.NotNil(t, result)
require.Empty(t, result.Id())
require.Empty(t, result.Token)
require.True(t, result.solvedAt.IsZero())
}