7
7
"io"
8
8
"net/http"
9
9
"testing"
10
+ "time"
10
11
11
12
"github.com/stretchr/testify/require"
12
13
@@ -17,10 +18,10 @@ import (
17
18
"gitlab.com/gitlab-org/gitlab-shell/v14/internal/gitlabnet/twofactorverify"
18
19
)
19
20
20
- func setup (t * testing.T ) []testserver.TestRequestHandler {
21
+ func setupManual (t * testing.T ) []testserver.TestRequestHandler {
21
22
requests := []testserver.TestRequestHandler {
22
23
{
23
- Path : "/api/v4/internal/two_factor_otp_check " ,
24
+ Path : "/api/v4/internal/two_factor_manual_otp_check " ,
24
25
Handler : func (w http.ResponseWriter , r * http.Request ) {
25
26
b , err := io .ReadAll (r .Body )
26
27
defer r .Body .Close ()
@@ -30,14 +31,15 @@ func setup(t *testing.T) []testserver.TestRequestHandler {
30
31
var requestBody * twofactorverify.RequestBody
31
32
require .NoError (t , json .Unmarshal (b , & requestBody ))
32
33
34
+ var body map [string ]interface {}
33
35
switch requestBody .KeyId {
34
36
case "1" :
35
- body : = map [string ]interface {}{
37
+ body = map [string ]interface {}{
36
38
"success" : true ,
37
39
}
38
40
json .NewEncoder (w ).Encode (body )
39
41
case "error" :
40
- body : = map [string ]interface {}{
42
+ body = map [string ]interface {}{
41
43
"success" : false ,
42
44
"message" : "error message" ,
43
45
}
@@ -47,18 +49,55 @@ func setup(t *testing.T) []testserver.TestRequestHandler {
47
49
}
48
50
},
49
51
},
52
+ {
53
+ Path : "/api/v4/internal/two_factor_push_otp_check" ,
54
+ Handler : func (w http.ResponseWriter , r * http.Request ) {
55
+ b , err := io .ReadAll (r .Body )
56
+ defer r .Body .Close ()
57
+
58
+ require .NoError (t , err )
59
+
60
+ var requestBody * twofactorverify.RequestBody
61
+ require .NoError (t , json .Unmarshal (b , & requestBody ))
62
+
63
+ time .Sleep (5 * time .Second )
64
+
65
+ var body map [string ]interface {}
66
+ switch requestBody .KeyId {
67
+ case "1" :
68
+ body = map [string ]interface {}{
69
+ "success" : true ,
70
+ }
71
+ json .NewEncoder (w ).Encode (body )
72
+ case "error" :
73
+ body = map [string ]interface {}{
74
+ "success" : false ,
75
+ "message" : "error message" ,
76
+ }
77
+ require .NoError (t , json .NewEncoder (w ).Encode (body ))
78
+ case "broken" :
79
+ w .WriteHeader (http .StatusInternalServerError )
80
+ default :
81
+ body = map [string ]interface {}{
82
+ "success" : true ,
83
+ "message" : "default message" ,
84
+ }
85
+ json .NewEncoder (w ).Encode (body )
86
+ }
87
+ },
88
+ },
50
89
}
51
90
52
91
return requests
53
92
}
54
93
55
94
const (
56
- question = "OTP: \n "
57
- errorHeader = "OTP validation failed.\n "
95
+ manualQuestion = "OTP: \n "
96
+ manualErrorHeader = "OTP validation failed.\n "
58
97
)
59
98
60
- func TestExecute (t * testing.T ) {
61
- requests := setup (t )
99
+ func TestExecuteManual (t * testing.T ) {
100
+ requests := setupManual (t )
62
101
63
102
url := testserver .StartSocketHttpServer (t , requests )
64
103
@@ -69,41 +108,35 @@ func TestExecute(t *testing.T) {
69
108
expectedOutput string
70
109
}{
71
110
{
72
- desc : "With a known key id" ,
73
- arguments : & commandargs.Shell {GitlabKeyId : "1" },
74
- answer : "123456\n " ,
75
- expectedOutput : question +
76
- "OTP validation successful. Git operations are now allowed.\n " ,
111
+ desc : "With a known key id" ,
112
+ arguments : & commandargs.Shell {GitlabKeyId : "1" },
113
+ answer : "123456\n " ,
114
+ expectedOutput : manualQuestion + "OTP validation successful. Git operations are now allowed.\n " ,
77
115
},
78
116
{
79
117
desc : "With bad response" ,
80
118
arguments : & commandargs.Shell {GitlabKeyId : "-1" },
81
119
answer : "123456\n " ,
82
- expectedOutput : question + errorHeader + "Parsing failed\n " ,
120
+ expectedOutput : manualQuestion + manualErrorHeader + "Parsing failed\n " ,
83
121
},
84
122
{
85
123
desc : "With API returns an error" ,
86
124
arguments : & commandargs.Shell {GitlabKeyId : "error" },
87
125
answer : "yes\n " ,
88
- expectedOutput : question + errorHeader + "error message\n " ,
126
+ expectedOutput : manualQuestion + manualErrorHeader + "error message\n " ,
89
127
},
90
128
{
91
129
desc : "With API fails" ,
92
130
arguments : & commandargs.Shell {GitlabKeyId : "broken" },
93
131
answer : "yes\n " ,
94
- expectedOutput : question + errorHeader + "Internal API error (500)\n " ,
95
- },
96
- {
97
- desc : "With missing arguments" ,
98
- arguments : & commandargs.Shell {},
99
- answer : "yes\n " ,
100
- expectedOutput : question + errorHeader + "who='' is invalid\n " ,
132
+ expectedOutput : manualQuestion + manualErrorHeader + "Internal API error (500)\n " ,
101
133
},
102
134
}
103
135
104
136
for _ , tc := range testCases {
105
137
t .Run (tc .desc , func (t * testing.T ) {
106
138
output := & bytes.Buffer {}
139
+
107
140
input := bytes .NewBufferString (tc .answer )
108
141
109
142
cmd := & Command {
0 commit comments