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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
package personalaccesstoken
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitlab-shell/v14/client/testserver"
"gitlab.com/gitlab-org/gitlab-shell/v14/internal/command/commandargs"
"gitlab.com/gitlab-org/gitlab-shell/v14/internal/command/readwriter"
"gitlab.com/gitlab-org/gitlab-shell/v14/internal/config"
"gitlab.com/gitlab-org/gitlab-shell/v14/internal/gitlabnet/personalaccesstoken"
)
var requests []testserver.TestRequestHandler
func setup(t *testing.T) {
requests = []testserver.TestRequestHandler{
{
Path: "/api/v4/internal/personal_access_token",
Handler: func(w http.ResponseWriter, r *http.Request) {
b, err := io.ReadAll(r.Body)
defer r.Body.Close()
require.NoError(t, err)
var requestBody *personalaccesstoken.RequestBody
json.Unmarshal(b, &requestBody)
switch requestBody.KeyId {
case "forbidden":
body := map[string]interface{}{
"success": false,
"message": "Forbidden!",
}
json.NewEncoder(w).Encode(body)
case "broken":
w.WriteHeader(http.StatusInternalServerError)
case "badresponse":
default:
var expiresAt interface{}
if requestBody.ExpiresAt == "" {
expiresAt = nil
} else {
expiresAt = "9001-11-17"
}
body := map[string]interface{}{
"success": true,
"token": "YXuxvUgCEmeePY3G1YAa",
"scopes": requestBody.Scopes,
"expires_at": expiresAt,
}
json.NewEncoder(w).Encode(body)
}
},
},
}
}
const (
cmdname = "personal_access_token"
)
func TestExecute(t *testing.T) {
setup(t)
url := testserver.StartSocketHttpServer(t, requests)
testCases := []struct {
desc string
arguments *commandargs.Shell
expectedOutput string
expectedError string
}{
{
desc: "Without any arguments",
arguments: &commandargs.Shell{},
expectedError: usageText,
},
{
desc: "With too few arguments",
arguments: &commandargs.Shell{
SshArgs: []string{cmdname, "newtoken"},
},
expectedError: usageText,
},
{
desc: "With too many arguments",
arguments: &commandargs.Shell{
SshArgs: []string{cmdname, "newtoken", "api", "bad_ttl", "toomany"},
},
expectedError: usageText,
},
{
desc: "With a bad ttl_days argument",
arguments: &commandargs.Shell{
SshArgs: []string{cmdname, "newtoken", "api", "bad_ttl"},
},
expectedError: "Invalid value for days_ttl: 'bad_ttl'",
},
{
desc: "Without a ttl argument",
arguments: &commandargs.Shell{
GitlabKeyId: "default",
SshArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
},
expectedOutput: "Token: YXuxvUgCEmeePY3G1YAa\n" +
"Scopes: read_api,read_repository\n" +
"Expires: 9001-11-17\n",
},
{
desc: "With a ttl argument",
arguments: &commandargs.Shell{
GitlabKeyId: "default",
SshArgs: []string{cmdname, "newtoken", "api", "30"},
},
expectedOutput: "Token: YXuxvUgCEmeePY3G1YAa\n" +
"Scopes: api\n" +
"Expires: 9001-11-17\n",
},
{
desc: "With bad response",
arguments: &commandargs.Shell{
GitlabKeyId: "badresponse",
SshArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
},
expectedError: "Parsing failed",
},
{
desc: "when API returns an error",
arguments: &commandargs.Shell{
GitlabKeyId: "forbidden",
SshArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
},
expectedError: "Forbidden!",
},
{
desc: "When API fails",
arguments: &commandargs.Shell{
GitlabKeyId: "broken",
SshArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
},
expectedError: "Internal API unreachable",
},
{
desc: "Without KeyID or User",
arguments: &commandargs.Shell{
SshArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
},
expectedError: "who='' is invalid",
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
output := &bytes.Buffer{}
input := bytes.NewBufferString("")
cmd := &Command{
Config: &config.Config{GitlabUrl: url},
Args: tc.arguments,
ReadWriter: &readwriter.ReadWriter{Out: output, In: input},
}
_, err := cmd.Execute(context.Background())
if tc.expectedError == "" {
require.NoError(t, err)
} else {
require.EqualError(t, err, tc.expectedError)
}
if tc.expectedOutput != "" {
require.Equal(t, tc.expectedOutput, output.String())
}
})
}
}
|