-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpath_role_pipeline_project_trigger_token_test.go
89 lines (81 loc) · 2.9 KB
/
path_role_pipeline_project_trigger_token_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
//go:build unit
package gitlab_test
import (
"cmp"
"fmt"
"os"
"testing"
"time"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/vault/sdk/logical"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
gitlab "github.com/ilijamt/vault-plugin-secrets-gitlab"
)
func TestPathRolesPipelineProjectTrigger(t *testing.T) {
var defaultConfig = map[string]any{
"token": "glpat-secret-random-token",
"base_url": cmp.Or(os.Getenv("GITLAB_URL"), "http://localhost:8080/"),
"type": gitlab.TypeSelfManaged.String(),
}
t.Run("should fail if have defined scopes or access level", func(t *testing.T) {
ctx := getCtxGitlabClient(t, "unit")
var b, l, err = getBackendWithConfig(ctx, defaultConfig)
require.NoError(t, err)
resp, err := b.HandleRequest(ctx, &logical.Request{
Operation: logical.CreateOperation,
Path: fmt.Sprintf("%s/%d", gitlab.PathRoleStorage, time.Now().UnixNano()), Storage: l,
Data: map[string]any{
"path": "user",
"name": "Example user personal token",
"access_level": gitlab.AccessLevelNoPermissions.String(),
"token_type": gitlab.TokenTypePipelineProjectTrigger.String(),
"scopes": []string{gitlab.TokenScopeApi.String()},
"ttl": "1h",
},
})
require.Error(t, err)
require.NotNil(t, resp)
var errorMap = countErrByName(err.(*multierror.Error))
assert.EqualValues(t, 2, errorMap[gitlab.ErrFieldInvalidValue.Error()])
})
t.Run("ttl is set", func(t *testing.T) {
ctx := getCtxGitlabClient(t, "unit")
var b, l, err = getBackendWithConfig(ctx, defaultConfig)
require.NoError(t, err)
resp, err := b.HandleRequest(ctx, &logical.Request{
Operation: logical.CreateOperation,
Path: fmt.Sprintf("%s/%d", gitlab.PathRoleStorage, time.Now().UnixNano()), Storage: l,
Data: map[string]any{
"path": "user",
"name": "Example user personal token",
"access_level": gitlab.AccessLevelUnknown.String(),
"token_type": gitlab.TokenTypePipelineProjectTrigger.String(),
"scopes": []string{},
"ttl": "1h",
},
})
require.NoError(t, err)
require.NotNil(t, resp)
require.EqualValues(t, 3600, resp.Data["ttl"])
})
t.Run("ttl is optional", func(t *testing.T) {
ctx := getCtxGitlabClient(t, "unit")
var b, l, err = getBackendWithConfig(ctx, defaultConfig)
require.NoError(t, err)
resp, err := b.HandleRequest(ctx, &logical.Request{
Operation: logical.CreateOperation,
Path: fmt.Sprintf("%s/%d", gitlab.PathRoleStorage, time.Now().UnixNano()), Storage: l,
Data: map[string]any{
"path": "user",
"name": "Example user personal token",
"access_level": gitlab.AccessLevelUnknown.String(),
"token_type": gitlab.TokenTypePipelineProjectTrigger.String(),
"scopes": []string{},
},
})
require.NoError(t, err)
require.NotNil(t, resp)
require.EqualValues(t, 0, resp.Data["ttl"])
})
}