77 "io"
88 "net/http"
99 "testing"
10+ "time"
1011
1112 "github.com/stretchr/testify/require"
1213
@@ -17,10 +18,10 @@ import (
1718 "gitlab.com/gitlab-org/gitlab-shell/v14/internal/gitlabnet/twofactorverify"
1819)
1920
20- func setup (t * testing.T ) []testserver.TestRequestHandler {
21+ func setupManual (t * testing.T ) []testserver.TestRequestHandler {
2122 requests := []testserver.TestRequestHandler {
2223 {
23- Path : "/api/v4/internal/two_factor_otp_check " ,
24+ Path : "/api/v4/internal/two_factor_manual_otp_check " ,
2425 Handler : func (w http.ResponseWriter , r * http.Request ) {
2526 b , err := io .ReadAll (r .Body )
2627 defer r .Body .Close ()
@@ -30,14 +31,15 @@ func setup(t *testing.T) []testserver.TestRequestHandler {
3031 var requestBody * twofactorverify.RequestBody
3132 require .NoError (t , json .Unmarshal (b , & requestBody ))
3233
34+ var body map [string ]interface {}
3335 switch requestBody .KeyId {
3436 case "1" :
35- body : = map [string ]interface {}{
37+ body = map [string ]interface {}{
3638 "success" : true ,
3739 }
3840 json .NewEncoder (w ).Encode (body )
3941 case "error" :
40- body : = map [string ]interface {}{
42+ body = map [string ]interface {}{
4143 "success" : false ,
4244 "message" : "error message" ,
4345 }
@@ -47,18 +49,55 @@ func setup(t *testing.T) []testserver.TestRequestHandler {
4749 }
4850 },
4951 },
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+ },
5089 }
5190
5291 return requests
5392}
5493
5594const (
56- question = "OTP: \n "
57- errorHeader = "OTP validation failed.\n "
95+ manualQuestion = "OTP: \n "
96+ manualErrorHeader = "OTP validation failed.\n "
5897)
5998
60- func TestExecute (t * testing.T ) {
61- requests := setup (t )
99+ func TestExecuteManual (t * testing.T ) {
100+ requests := setupManual (t )
62101
63102 url := testserver .StartSocketHttpServer (t , requests )
64103
@@ -69,41 +108,35 @@ func TestExecute(t *testing.T) {
69108 expectedOutput string
70109 }{
71110 {
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 " ,
77115 },
78116 {
79117 desc : "With bad response" ,
80118 arguments : & commandargs.Shell {GitlabKeyId : "-1" },
81119 answer : "123456\n " ,
82- expectedOutput : question + errorHeader + "Parsing failed\n " ,
120+ expectedOutput : manualQuestion + manualErrorHeader + "Parsing failed\n " ,
83121 },
84122 {
85123 desc : "With API returns an error" ,
86124 arguments : & commandargs.Shell {GitlabKeyId : "error" },
87125 answer : "yes\n " ,
88- expectedOutput : question + errorHeader + "error message\n " ,
126+ expectedOutput : manualQuestion + manualErrorHeader + "error message\n " ,
89127 },
90128 {
91129 desc : "With API fails" ,
92130 arguments : & commandargs.Shell {GitlabKeyId : "broken" },
93131 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 " ,
101133 },
102134 }
103135
104136 for _ , tc := range testCases {
105137 t .Run (tc .desc , func (t * testing.T ) {
106138 output := & bytes.Buffer {}
139+
107140 input := bytes .NewBufferString (tc .answer )
108141
109142 cmd := & Command {
0 commit comments