Skip to content

Commit dca305e

Browse files
authored
fix: make groupaccesstokens expirydate nullable (#241)
* fix: make groupaccesstokens expirydate nullable Signed-off-by: ralf.weber <[email protected]> * fix: Remove hard-coded expiry default Signed-off-by: ralf.weber <[email protected]> --------- Signed-off-by: ralf.weber <[email protected]>
1 parent 3f5c519 commit dca305e

File tree

6 files changed

+226
-6
lines changed

6 files changed

+226
-6
lines changed

apis/cluster/groups/v1alpha1/zz_accesstoken_types.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apis/namespaced/groups/v1alpha1/accesstoken_types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ type AccessTokenParameters struct {
4141
GroupIDSelector *xpv1.NamespacedSelector `json:"groupIdSelector,omitempty"`
4242

4343
// Expiration date of the access token. The date cannot be set later than the maximum allowable lifetime of an access token.
44-
// If not set, the maximum allowable lifetime of a personal access token is 365 days.
44+
// If not set, the maximum allowable lifetime of a group access token is configured to the maximum allowable lifetime limit.
4545
// Expected in ISO 8601 format (2019-03-15T08:00:00Z)
4646
// +immutable
47-
ExpiresAt *metav1.Time `json:"expiresAt,omitempty"`
47+
ExpiresAt *metav1.Time `json:"expiresAt"`
4848

4949
// Access level for the group. Default is 40.
5050
// Valid values are 10 (Guest), 20 (Reporter), 30 (Developer), 40 (Maintainer), and 50 (Owner).

package/crds/groups.gitlab.crossplane.io_accesstokens.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ spec:
8181
expiresAt:
8282
description: |-
8383
Expiration date of the access token. The date cannot be set later than the maximum allowable lifetime of an access token.
84-
If not set, the maximum allowable lifetime of a personal access token is 365 days.
84+
If not set, the maximum allowable lifetime of a group access token is configured to the maximum allowable lifetime limit.
8585
Expected in ISO 8601 format (2019-03-15T08:00:00Z)
8686
format: date-time
8787
type: string
@@ -177,6 +177,7 @@ spec:
177177
type: string
178178
type: array
179179
required:
180+
- expiresAt
180181
- name
181182
- scopes
182183
type: object

package/crds/groups.gitlab.m.crossplane.io_accesstokens.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ spec:
6767
expiresAt:
6868
description: |-
6969
Expiration date of the access token. The date cannot be set later than the maximum allowable lifetime of an access token.
70-
If not set, the maximum allowable lifetime of a personal access token is 365 days.
70+
If not set, the maximum allowable lifetime of a group access token is configured to the maximum allowable lifetime limit.
7171
Expected in ISO 8601 format (2019-03-15T08:00:00Z)
7272
format: date-time
7373
type: string
@@ -169,6 +169,7 @@ spec:
169169
type: string
170170
type: array
171171
required:
172+
- expiresAt
172173
- name
173174
- scopes
174175
type: object

pkg/cluster/clients/groups/zz_accesstoken_test.go

Lines changed: 110 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
Copyright 2021 The Crossplane Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package groups
18+
19+
import (
20+
"testing"
21+
"time"
22+
23+
"github.com/google/go-cmp/cmp"
24+
gitlab "gitlab.com/gitlab-org/api/client-go"
25+
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
27+
"github.com/crossplane-contrib/provider-gitlab/apis/namespaced/groups/v1alpha1"
28+
)
29+
30+
func TestGenerateCreateGroupAccessTokenOptions(t *testing.T) {
31+
name := "Name"
32+
var expiresAt time.Time
33+
scopes := []string{"scope1", "scope2"}
34+
accessLevel := 40
35+
type args struct {
36+
name string
37+
parameters *v1alpha1.AccessTokenParameters
38+
}
39+
cases := map[string]struct {
40+
args args
41+
want *gitlab.CreateGroupAccessTokenOptions
42+
}{
43+
"AllFields": {
44+
args: args{
45+
name: name,
46+
parameters: &v1alpha1.AccessTokenParameters{
47+
Name: name,
48+
AccessLevel: (*v1alpha1.AccessLevelValue)(&accessLevel),
49+
ExpiresAt: &v1.Time{Time: expiresAt},
50+
Scopes: scopes,
51+
},
52+
},
53+
want: &gitlab.CreateGroupAccessTokenOptions{
54+
Name: &name,
55+
AccessLevel: (*gitlab.AccessLevelValue)(&accessLevel),
56+
ExpiresAt: (*gitlab.ISOTime)(&expiresAt),
57+
Scopes: &scopes,
58+
},
59+
},
60+
"noExpiresAt": {
61+
args: args{
62+
name: name,
63+
parameters: &v1alpha1.AccessTokenParameters{
64+
Name: name,
65+
AccessLevel: (*v1alpha1.AccessLevelValue)(&accessLevel),
66+
ExpiresAt: nil,
67+
Scopes: scopes,
68+
},
69+
},
70+
want: &gitlab.CreateGroupAccessTokenOptions{
71+
Name: &name,
72+
AccessLevel: (*gitlab.AccessLevelValue)(&accessLevel),
73+
ExpiresAt: nil,
74+
Scopes: &scopes,
75+
},
76+
},
77+
}
78+
for name, tc := range cases {
79+
t.Run(name, func(t *testing.T) {
80+
got := GenerateCreateGroupAccessTokenOptions(tc.args.name, tc.args.parameters)
81+
82+
// Compare fields individually since gitlab.ISOTime has unexported fields
83+
if tc.want.Name != nil && got.Name != nil && *tc.want.Name != *got.Name {
84+
t.Errorf("Name: want %v, got %v", *tc.want.Name, *got.Name)
85+
}
86+
87+
if tc.want.AccessLevel != nil && got.AccessLevel != nil && *tc.want.AccessLevel != *got.AccessLevel {
88+
t.Errorf("AccessLevel: want %v, got %v", *tc.want.AccessLevel, *got.AccessLevel)
89+
}
90+
91+
if tc.want.ExpiresAt != nil && got.ExpiresAt != nil {
92+
wantTime := time.Time(*tc.want.ExpiresAt)
93+
gotTime := time.Time(*got.ExpiresAt)
94+
wantDay := wantTime.Truncate(24 * time.Hour)
95+
gotDay := gotTime.Truncate(24 * time.Hour)
96+
if !wantDay.Equal(gotDay) {
97+
t.Errorf("ExpiresAt: want %v, got %v", wantDay, gotDay)
98+
}
99+
} else if tc.want.ExpiresAt != got.ExpiresAt {
100+
t.Errorf("ExpiresAt: want %v, got %v", tc.want.ExpiresAt, got.ExpiresAt)
101+
}
102+
103+
if diff := cmp.Diff(tc.want.Scopes, got.Scopes); diff != "" {
104+
t.Errorf("Scopes: -want, +got:\n%s", diff)
105+
}
106+
})
107+
}
108+
}

0 commit comments

Comments
 (0)