@@ -23,51 +23,73 @@ func TestProfile(t *testing.T) {
23
23
suite .Run (t , new (ProfileTestSuite ))
24
24
}
25
25
26
- func (s * ProfileTestSuite ) TestProfileLifecycle () {
26
+ // createProfileFixture creates a profile with the given default flag.
27
+ // Note: only non default profiles can be deleted.
28
+ func (s * ProfileTestSuite ) createProfileFixture (isDefault bool ) * billing.Profile {
29
+ t := s .T ()
27
30
ctx := context .Background ()
28
- ns := "test_create_billing_profile"
31
+ ns := s . GetUniqueNamespace ( "test_billing_profile" )
29
32
30
- _ = s .InstallSandboxApp (s .T (), ns )
33
+ // Create a profile input
34
+ input := MinimalCreateProfileInputTemplate
35
+ input .Namespace = ns
36
+ input .Default = isDefault
37
+
38
+ // Create a sandbox app
39
+ app := s .InstallSandboxApp (s .T (), ns )
40
+ require .NotNil (t , app )
41
+
42
+ // Create a default profile
43
+ profile , err := s .BillingService .CreateProfile (ctx , input )
44
+ require .NoError (t , err )
45
+ require .NotNil (t , profile )
46
+
47
+ profile .CreatedAt = profile .CreatedAt .Truncate (time .Microsecond )
48
+ profile .UpdatedAt = profile .UpdatedAt .Truncate (time .Microsecond )
49
+ profile .WorkflowConfig .CreatedAt = profile .WorkflowConfig .CreatedAt .Truncate (time .Microsecond )
50
+ profile .WorkflowConfig .UpdatedAt = profile .WorkflowConfig .UpdatedAt .Truncate (time .Microsecond )
51
+
52
+ return profile
53
+ }
54
+
55
+ func (s * ProfileTestSuite ) TestProfileLifecycle () {
56
+ ctx := context .Background ()
31
57
32
58
s .T ().Run ("missing default profile" , func (t * testing.T ) {
59
+ profile := s .createProfileFixture (false )
60
+
33
61
defaultProfile , err := s .BillingService .GetDefaultProfile (ctx , billing.GetDefaultProfileInput {
34
- Namespace : ns ,
62
+ Namespace : profile . Namespace ,
35
63
})
36
64
require .NoError (t , err )
37
65
require .Nil (t , defaultProfile )
38
66
})
39
67
40
- var profile * billing.Profile
41
- var err error
42
-
43
- minimalCreateProfileInput := MinimalCreateProfileInputTemplate
44
- minimalCreateProfileInput .Namespace = ns
45
-
46
68
s .T ().Run ("create default profile" , func (t * testing.T ) {
47
- profile , err = s .BillingService . CreateProfile ( ctx , minimalCreateProfileInput )
69
+ profile : = s .createProfileFixture ( true )
48
70
49
- require .NoError (t , err )
50
71
require .NotNil (t , profile )
72
+ require .True (t , profile .Default )
51
73
})
52
74
53
- profile .CreatedAt = profile .CreatedAt .Truncate (time .Microsecond )
54
- profile .UpdatedAt = profile .UpdatedAt .Truncate (time .Microsecond )
55
- profile .WorkflowConfig .CreatedAt = profile .WorkflowConfig .CreatedAt .Truncate (time .Microsecond )
56
- profile .WorkflowConfig .UpdatedAt = profile .WorkflowConfig .UpdatedAt .Truncate (time .Microsecond )
57
-
58
75
s .T ().Run ("fetching the default profile is possible" , func (t * testing.T ) {
76
+ profile := s .createProfileFixture (true )
77
+
59
78
defaultProfile , err := s .BillingService .GetDefaultProfile (ctx , billing.GetDefaultProfileInput {
60
- Namespace : ns ,
79
+ Namespace : profile . Namespace ,
61
80
})
81
+
62
82
require .NoError (t , err )
63
83
require .NotNil (t , defaultProfile )
64
84
require .Equal (t , profile , defaultProfile )
65
85
})
66
86
67
87
s .T ().Run ("fetching the profile by id" , func (t * testing.T ) {
88
+ profile := s .createProfileFixture (false )
89
+
68
90
fetchedProfile , err := s .BillingService .GetProfile (ctx , billing.GetProfileInput {
69
91
Profile : models.NamespacedID {
70
- Namespace : ns ,
92
+ Namespace : profile . Namespace ,
71
93
ID : profile .ID ,
72
94
},
73
95
Expand : billing.ProfileExpand {
@@ -80,56 +102,89 @@ func (s *ProfileTestSuite) TestProfileLifecycle() {
80
102
})
81
103
82
104
s .T ().Run ("creating a second default profile fails" , func (t * testing.T ) {
83
- _ , err := s .BillingService .CreateProfile (ctx , minimalCreateProfileInput )
105
+ profile := s .createProfileFixture (true )
106
+
107
+ // Try to create a second default profile in the same namespace
108
+ input := MinimalCreateProfileInputTemplate
109
+ input .Namespace = profile .Namespace
110
+
111
+ _ , err := s .BillingService .CreateProfile (ctx , input )
84
112
require .Error (t , err )
85
113
require .ErrorIs (t , err , billing .ErrDefaultProfileAlreadyExists )
86
114
})
87
115
88
116
s .T ().Run ("creating a second default profile succeeds with override" , func (t * testing.T ) {
89
- overrideInput := minimalCreateProfileInput
90
- overrideInput .DefaultOverride = true
117
+ profile1 := s .createProfileFixture (true )
118
+
119
+ // Create a second default profile with override
120
+ input := MinimalCreateProfileInputTemplate
121
+ input .Namespace = profile1 .Namespace
122
+ input .DefaultOverride = true
91
123
92
- profile , err := s .BillingService .CreateProfile (ctx , overrideInput )
124
+ profile2 , err := s .BillingService .CreateProfile (ctx , input )
93
125
require .NoError (t , err )
126
+ require .NotNil (t , profile2 )
94
127
95
- // Cleanup
96
- require .NoError (t , s .BillingService .DeleteProfile (ctx , billing.DeleteProfileInput {
97
- Namespace : ns ,
98
- ID : profile .ID ,
99
- }))
128
+ require .NotEqual (t , profile1 .ID , profile2 .ID )
129
+ require .True (t , profile2 .Default )
100
130
})
101
131
102
132
s .T ().Run ("deleted profile handling" , func (t * testing.T ) {
103
- require .NoError (t , s .BillingService .DeleteProfile (ctx , billing.DeleteProfileInput {
104
- Namespace : ns ,
105
- ID : profile .ID ,
106
- }))
133
+ t .Run ("deleting a profile succeeds" , func (t * testing.T ) {
134
+ profile := s .createProfileFixture (false )
135
+
136
+ require .NoError (t , s .BillingService .DeleteProfile (ctx , billing.DeleteProfileInput {
137
+ Namespace : profile .Namespace ,
138
+ ID : profile .ID ,
139
+ }))
140
+ })
107
141
108
142
t .Run ("deleting a profile twice yields an error" , func (t * testing.T ) {
143
+ profile := s .createProfileFixture (false )
144
+
145
+ // Delete the profile
146
+ require .NoError (t , s .BillingService .DeleteProfile (ctx , billing.DeleteProfileInput {
147
+ Namespace : profile .Namespace ,
148
+ ID : profile .ID ,
149
+ }))
150
+
151
+ // Try to delete the profile again
109
152
require .ErrorIs (t , s .BillingService .DeleteProfile (ctx , billing.DeleteProfileInput {
110
- Namespace : ns ,
153
+ Namespace : profile . Namespace ,
111
154
ID : profile .ID ,
112
155
}), billing .ErrProfileAlreadyDeleted )
113
156
})
114
157
158
+ t .Run ("deleting the default profile yields an error" , func (t * testing.T ) {
159
+ profile := s .createProfileFixture (true )
160
+
161
+ // Try to delete the default profile
162
+ require .ErrorIs (t , s .BillingService .DeleteProfile (ctx , billing.DeleteProfileInput {
163
+ Namespace : profile .Namespace ,
164
+ ID : profile .ID ,
165
+ }), billing .ErrDefaultProfileCannotBeDeleted )
166
+ })
167
+
115
168
t .Run ("fetching a deleted profile by id returns the profile" , func (t * testing.T ) {
169
+ profile := s .createProfileFixture (false )
170
+
171
+ // Delete the profile
172
+ require .NoError (t , s .BillingService .DeleteProfile (ctx , billing.DeleteProfileInput {
173
+ Namespace : profile .Namespace ,
174
+ ID : profile .ID ,
175
+ }))
176
+
177
+ // Fetch the profile
116
178
fetchedProfile , err := s .BillingService .GetProfile (ctx , billing.GetProfileInput {
117
179
Profile : models.NamespacedID {
118
- Namespace : ns ,
180
+ Namespace : profile . Namespace ,
119
181
ID : profile .ID ,
120
182
},
121
183
})
122
184
123
185
require .NoError (t , err )
124
186
require .Equal (t , profile .ID , fetchedProfile .ID )
125
187
})
126
-
127
- t .Run ("same profile can be created after the previous one was deleted" , func (t * testing.T ) {
128
- profile , err = s .BillingService .CreateProfile (ctx , minimalCreateProfileInput )
129
-
130
- require .NoError (t , err )
131
- require .NotNil (t , profile )
132
- })
133
188
})
134
189
}
135
190
0 commit comments