-
Notifications
You must be signed in to change notification settings - Fork 1
/
group_test.go
141 lines (121 loc) · 4.38 KB
/
group_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
134
135
136
137
138
139
140
141
package gcloudcx_test
import (
"context"
"fmt"
"reflect"
"strings"
"testing"
"time"
"github.com/gildas/go-core"
"github.com/gildas/go-gcloudcx"
"github.com/gildas/go-logger"
"github.com/google/uuid"
"github.com/joho/godotenv"
"github.com/stretchr/testify/suite"
)
type GroupSuite struct {
suite.Suite
Name string
Logger *logger.Logger
Start time.Time
GroupID uuid.UUID
GroupName string
Client *gcloudcx.Client
}
func TestGroupSuite(t *testing.T) {
suite.Run(t, new(GroupSuite))
}
// *****************************************************************************
// #region: Suite Tools {{{
func (suite *GroupSuite) SetupSuite() {
var err error
var value string
_ = 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", "")
clientID = uuid.MustParse(core.GetEnvAsString("PURECLOUD_CLIENTID", ""))
secret = core.GetEnvAsString("PURECLOUD_CLIENTSECRET", "")
deploymentID = uuid.MustParse(core.GetEnvAsString("PURECLOUD_DEPLOYMENTID", ""))
)
suite.Client = gcloudcx.NewClient(&gcloudcx.ClientOptions{
Region: region,
DeploymentID: deploymentID,
Logger: suite.Logger,
}).SetAuthorizationGrant(&gcloudcx.ClientCredentialsGrant{
ClientID: clientID,
Secret: secret,
})
value = core.GetEnvAsString("GROUP_ID", "")
suite.Require().NotEmpty(value, "GROUP_ID is not set in your environment")
suite.GroupID, err = uuid.Parse(value)
suite.Require().NoError(err, "GROUP_ID is not a valid UUID")
suite.GroupName = core.GetEnvAsString("GROUP_NAME", "")
suite.Require().NotEmpty(suite.GroupName, "GROUP_NAME is not set in your environment")
suite.Require().NotNil(suite.Client, "GCloudCX Client is nil")
}
func (suite *GroupSuite) 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 *GroupSuite) BeforeTest(suiteName, testName string) {
suite.Logger.Infof("Test Start: %s %s", testName, strings.Repeat("-", 80-13-len(testName)))
suite.Start = time.Now()
// Reuse tokens as much as we can
if !suite.Client.IsAuthorized() {
suite.Logger.Infof("Client is not logged in...")
err := suite.Client.Login(context.Background())
suite.Require().NoError(err, "Failed to login")
suite.Logger.Infof("Client is now logged in...")
} else {
suite.Logger.Infof("Client is already logged in...")
}
}
func (suite *GroupSuite) 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 *GroupSuite) TestCanFetchByID() {
group, err := gcloudcx.Fetch[gcloudcx.Group](context.Background(), suite.Client, suite.GroupID)
suite.Require().NoErrorf(err, "Failed to fetch Group %s. %s", suite.GroupID, err)
suite.Assert().Equal(suite.GroupID, group.ID)
suite.Assert().Equal(suite.GroupName, group.Name)
suite.Assert().Equal("public", group.Visibility)
}
func (suite *GroupSuite) TestCanFetchByName() {
match := func(group gcloudcx.Group) bool {
return group.Name == suite.GroupName
}
group, err := gcloudcx.FetchBy(context.Background(), suite.Client, match)
suite.Require().NoErrorf(err, "Failed to fetch Group %s. %s", suite.GroupName, err)
suite.Assert().Equal(suite.GroupID, group.ID)
suite.Assert().Equal(suite.GroupName, group.Name)
suite.Assert().Equal("public", group.Visibility)
}
func (suite *GroupSuite) TestCanStringify() {
id := uuid.New()
group := gcloudcx.Group{
ID: id,
Name: "Hello",
}
suite.Assert().Equal("Hello", group.String())
group.Name = ""
suite.Assert().Equal(id.String(), group.String())
}