-
Notifications
You must be signed in to change notification settings - Fork 1
/
login_test.go
133 lines (113 loc) · 4.46 KB
/
login_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
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
//go:build integration
// +build integration
package gcloudcx_test
import (
"context"
"fmt"
"reflect"
"strings"
"testing"
"time"
"github.com/gildas/go-core"
"github.com/gildas/go-errors"
"github.com/gildas/go-logger"
"github.com/google/uuid"
"github.com/joho/godotenv"
"github.com/stretchr/testify/suite"
"github.com/gildas/go-gcloudcx"
)
type LoginSuite struct {
suite.Suite
Name string
Logger *logger.Logger
Start time.Time
Client *gcloudcx.Client
}
func TestLoginSuite(t *testing.T) {
suite.Run(t, new(LoginSuite))
}
// *****************************************************************************
// #region: Suite Tools {{{
func (suite *LoginSuite) SetupSuite() {
_ = godotenv.Load()
suite.Name = strings.TrimSuffix(reflect.TypeOf(suite).Elem().Name(), "Suite")
suite.Logger = logger.Create("test",
&logger.FileStream{
Path: fmt.Sprintf("./log/test-%s.log", strings.ToLower(suite.Name)),
Unbuffered: true,
SourceInfo: true,
FilterLevels: logger.NewLevelSet(logger.TRACE),
},
).Child("test", "test")
suite.Logger.Infof("Suite Start: %s %s", suite.Name, strings.Repeat("=", 80-14-len(suite.Name)))
var (
region = core.GetEnvAsString("PURECLOUD_REGION", "")
deploymentID = uuid.MustParse(core.GetEnvAsString("PURECLOUD_DEPLOYMENTID", ""))
)
suite.Client = gcloudcx.NewClient(&gcloudcx.ClientOptions{
Region: region,
DeploymentID: deploymentID,
Logger: suite.Logger,
})
suite.Require().NotNil(suite.Client, "GCloudCX Client is nil")
}
func (suite *LoginSuite) TearDownSuite() {
if suite.T().Failed() {
suite.Logger.Warnf("At least one test failed, we are not cleaning")
suite.T().Log("At least one test failed, we are not cleaning")
} else {
suite.Logger.Infof("All tests succeeded, we are cleaning")
}
suite.Logger.Infof("Suite End: %s %s", suite.Name, strings.Repeat("=", 80-12-len(suite.Name)))
suite.Logger.Close()
}
func (suite *LoginSuite) BeforeTest(suiteName, testName string) {
suite.Logger.Infof("Test Start: %s %s", testName, strings.Repeat("-", 80-13-len(testName)))
suite.Start = time.Now()
}
func (suite *LoginSuite) AfterTest(suiteName, testName string) {
duration := time.Since(suite.Start)
suite.Logger.Record("duration", duration.String()).Infof("Test End: %s %s", testName, strings.Repeat("-", 80-11-len(testName)))
}
// #endregion: Suite Tools }}}
// *****************************************************************************
func (suite *LoginSuite) TestCanLogin() {
err := suite.Client.SetAuthorizationGrant(&gcloudcx.ClientCredentialsGrant{
ClientID: uuid.MustParse(core.GetEnvAsString("PURECLOUD_CLIENTID", "")),
Secret: core.GetEnvAsString("PURECLOUD_CLIENTSECRET", ""),
}).Login(context.Background())
suite.Assert().NoError(err, "Failed to login")
}
func (suite *LoginSuite) TestFailsLoginWithInvalidClientID() {
err := suite.Client.LoginWithAuthorizationGrant(context.Background(), &gcloudcx.ClientCredentialsGrant{
ClientID: uuid.New(), // that UUID should not be anywhere in GCloud
Secret: core.GetEnvAsString("PURECLOUD_CLIENTSECRET", ""),
})
suite.Assert().Error(err, "Should have failed login in")
var apierr gcloudcx.APIError
ok := errors.As(err, &apierr)
suite.Require().Truef(ok, "Error is not a gcloudcx.APIError, error: %+v", err)
suite.Logger.Record("apierr", apierr).Errorf("API Error", err)
suite.Assert().Equal(errors.HTTPBadRequest.Code, apierr.Status)
suite.Assert().Equal("client not found: invalid_client", apierr.Error())
}
func (suite *LoginSuite) TestFailsLoginWithInvalidSecret() {
err := suite.Client.LoginWithAuthorizationGrant(context.Background(), &gcloudcx.ClientCredentialsGrant{
ClientID: uuid.MustParse(core.GetEnvAsString("PURECLOUD_CLIENTID", "")),
Secret: "WRONGSECRET",
})
suite.Assert().Error(err, "Should have failed login in")
var apierr gcloudcx.APIError
ok := errors.As(err, &apierr)
suite.Require().Truef(ok, "Error is not a gcloudcx.APIError, error: %+v", err)
suite.Logger.Record("apierr", apierr).Errorf("API Error", err)
suite.Assert().Equal(errors.HTTPUnauthorized.Code, apierr.Status)
suite.Assert().Equal("authentication failed: invalid_client", apierr.Error())
}
func (suite *LoginSuite) TestCanLoginWithClientCredentialsGrant() {
err := suite.Client.LoginWithAuthorizationGrant(context.Background(), &gcloudcx.ClientCredentialsGrant{
ClientID: uuid.MustParse(core.GetEnvAsString("PURECLOUD_CLIENTID", "")),
Secret: core.GetEnvAsString("PURECLOUD_CLIENTSECRET", ""),
})
suite.Assert().NoError(err, "Failed to login")
}