1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-30 05:59:39 +02:00
sap-jenkins-library/pkg/gcp/token_test.go
Googlom af05acad58
feat(events): Publish events to GCP PubSub by each step (#5122)
* test

* test

* draft

* generator

* some polishing

* go mod tidy

* fix unit

* fix unit

* fix unit

* fix unit

* fix unit

* resolve review comments

* resolve review comments

* add debug message on successful publish

* refactor to use global vault client

* cleanup

* rename

* clenup

* refactor

* remove token revocation

* handle nil vaultClient and add comments

* feat(events): Publish events (generated part) (#5131)

* add generated

* add generated

* refactor vaultClient usage

* fix unit tests

* fix unit tests

* fix
2024-10-11 14:55:39 +05:00

68 lines
1.5 KiB
Go

package gcp
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestGetExchangeTokenRequestData(t *testing.T) {
// ctx := context.Background()
t.Run("success", func(t *testing.T) {
// init
projectNumber := "PROJECT_NUMBER"
pool := "POOL"
provider := "PROVIDER"
// test
data := getExchangeTokenRequestData(projectNumber, pool, provider, mock.Anything)
// asserts
assert.Equal(t, data.Audience, "//iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/POOL/providers/PROVIDER")
assert.Equal(t, data.SubjectToken, mock.Anything)
})
}
func Test_tokenIsValid(t *testing.T) {
nowUnix := time.Now().Unix()
tests := []struct {
name string
token string
expiresAtStr string
want bool
}{
{
"token is empty",
"",
"",
false,
}, {
"token expiredAt is empty",
"someToken",
"",
false,
}, {
"token is expired",
"someToken",
fmt.Sprintf("%d", nowUnix-100), // expiresAt is 100 seconds ahead
false,
}, {
"token is expired inside buffered timeframe",
"someToken",
fmt.Sprintf("%d", nowUnix+3), // expiresAt is 3 seconds before
false,
}, {
"token is valid",
"someToken",
fmt.Sprintf("%d", nowUnix+100), // expiresAt is 100 seconds before
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tokenIsValid(tt.token, tt.expiresAtStr), "tokenIsValid(%v, %v)", tt.token, tt.expiresAtStr)
})
}
}