-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
214 lines (176 loc) · 8.78 KB
/
client_test.go
File metadata and controls
214 lines (176 loc) · 8.78 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package tripletex
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/valuetechdev/tripletex-go/fieldsbuilder"
)
func TestNewClient(t *testing.T) {
require := require.New(t)
baseURL := mustEnv("TRIPLETEX_BASE_URL")
consumerToken := mustEnv("TRIPLETEX_CONSUMER_TOKEN")
employeeToken := mustEnv("TRIPLETEX_EMPLOYEE_TOKEN")
creds := Credentials{
ConsumerToken: consumerToken,
EmployeeToken: employeeToken,
}
c := New(creds, WithBaseURLOption(baseURL))
require.False(c.IsTokenValid())
require.NoError(c.CheckAuth())
lastYear, err := time.Parse(time.DateOnly, "2024-01-01")
require.NoError(err)
lastYearString := lastYear.Format(time.RFC3339)
fields := fieldsbuilder.New().Single("name").Single("id").String()
customersRes, err := c.CustomerSearchWithResponse(context.Background(), &CustomerSearchParams{ChangedSince: &lastYearString, Fields: &fields})
require.NoError(err)
require.NotNil(customersRes)
}
func TestNewClientWithAccountantClient(t *testing.T) {
require := require.New(t)
baseURL := mustEnv("TRIPLETEX_BASE_URL_ACCOUNTANT")
consumerToken := mustEnv("TRIPLETEX_CONSUMER_TOKEN_ACCOUNTANT")
employeeToken := mustEnv("TRIPLETEX_EMPLOYEE_TOKEN_ACCOUNTANT")
creds := Credentials{
ConsumerToken: consumerToken,
EmployeeToken: employeeToken,
}
c := New(creds, WithBaseURLOption(baseURL))
require.False(c.IsTokenValid(), "token should be invalid after init")
require.NoError(c.CheckAuth(), "should be able to check token after init")
require.True(c.IsTokenValid(), "token should be valid after check")
res, err := c.CompanyWithLoginAccessGetWithLoginAccessWithResponse(context.Background(), &CompanyWithLoginAccessGetWithLoginAccessParams{})
require.NoError(err, "should not error for getting companies with login")
require.NotNil(res, "companies with login should not be nil")
require.NotNil(res.JSONDefault, "companies JSONDefault with login should not be nil")
require.NotNil(res.JSONDefault.Values, "companies JSONDefault.Values with login should not be nil")
whoAmIRes, err := c.TokenSessionWhoAmIWhoAmIWithResponse(context.Background(), &TokenSessionWhoAmIWhoAmIParams{})
require.NoError(err, "should not error for checking whoAmI")
require.NotNil(whoAmIRes, "whoAmI should not be nil")
require.Equal(http.StatusOK, whoAmIRes.StatusCode(), "whoAmI status should be OK (200)")
require.NotNil(whoAmIRes.JSONDefault, "whoAmI.JSONDefault value should not be nil")
require.NotNil(whoAmIRes.JSONDefault.Value, "whoAmI.JSONDefault.Value should not be nil")
t.Logf("Available companies with login access:")
for i, company := range *res.JSONDefault.Values {
t.Logf(" %d: ID=%d, Name=%s", i, *company.Id, *company.Name)
}
validClientId := *(*res.JSONDefault.Values)[0].Id
t.Logf("Using clientId: %d", validClientId)
clientId := int64(validClientId)
c = New(creds, WithBaseURLOption(baseURL), WithAccountantClient(clientId))
require.False(c.IsTokenValid(), "token should be invalid after init")
require.NoError(c.CheckAuth(), "should be able to check token after init")
require.True(c.IsTokenValid(), "token should be valid after check")
whoAmIRes2, err := c.TokenSessionWhoAmIWhoAmIWithResponse(context.Background(), &TokenSessionWhoAmIWhoAmIParams{})
require.NoError(err, "whoAmI with accountant client should not error")
require.NotNil(whoAmIRes2, "whoAmI with accountant client should not be nil")
require.Equal(http.StatusOK, whoAmIRes2.StatusCode(), "whoAmI status should be OK (200)")
departmentRes, err := c.DepartmentSearchWithResponse(context.Background(), &DepartmentSearchParams{})
require.NoError(err, "departments search should not error")
require.NotNil(departmentRes, "departments res should not be nil")
if departmentRes.StatusCode() != http.StatusOK {
t.Logf("departments search failed with status: %d", departmentRes.StatusCode())
if departmentRes.Body != nil {
t.Logf("Response body: %s", string(departmentRes.Body))
}
}
require.Equal(http.StatusOK, departmentRes.StatusCode(), "departments status should be OK (200)")
require.NotNil(departmentRes.JSONDefault, "departments res.JSONDefault should not be nil")
require.NotNil(departmentRes.JSONDefault.Values, "departments res.JSONDefault.Values should not be nil")
}
func TestEmployeeTokenEntitlements(t *testing.T) {
require := require.New(t)
baseURL := mustEnv("TRIPLETEX_BASE_URL")
consumerToken := mustEnv("TRIPLETEX_CONSUMER_TOKEN")
employeeToken := mustEnv("TRIPLETEX_EMPLOYEE_TOKEN")
creds := Credentials{
ConsumerToken: consumerToken,
EmployeeToken: employeeToken,
}
c := New(creds, WithBaseURLOption(baseURL))
require.NoError(c.CheckAuth())
// Step 1: Get whoAmI to find employeeId and actualEmployeeId
whoAmIRes, err := c.TokenSessionWhoAmIWhoAmIWithResponse(context.Background(), &TokenSessionWhoAmIWhoAmIParams{})
require.NoError(err, "whoAmI should not error")
require.NotNil(whoAmIRes.JSONDefault, "whoAmI JSONDefault should not be nil")
require.NotNil(whoAmIRes.JSONDefault.Value, "whoAmI value should not be nil")
j, _ := json.MarshalIndent(whoAmIRes.JSONDefault.Value, "", " ")
t.Logf("whoAmIRes: %s", string(j))
employeeId := int64(*whoAmIRes.JSONDefault.Value.EmployeeId)
actualEmployeeId := int64(*whoAmIRes.JSONDefault.Value.ActualEmployeeId)
require.NotNil(employeeId, "employeeId should not be nil")
require.NotNil(actualEmployeeId, "actualEmployeeId should not be nil")
t.Logf("Employee ID (owner): %d", employeeId)
t.Logf("Actual Employee ID (token): %d", actualEmployeeId)
// Step 2: Try to get entitlements - this may fail due to permissions
ownerEntitlements, err := c.EmployeeEntitlementSearchWithResponse(context.Background(), &EmployeeEntitlementSearchParams{EmployeeId: &employeeId})
require.NoError(err, "owner entitlements should not error")
require.Equal(http.StatusOK, ownerEntitlements.StatusCode(), "owner entitlements status should be OK (200)")
tokenEntitlements, err := c.EmployeeEntitlementSearchWithResponse(context.Background(), &EmployeeEntitlementSearchParams{EmployeeId: &actualEmployeeId})
require.NoError(err, "token entitlements should not error")
require.Equal(http.StatusOK, tokenEntitlements.StatusCode(), "token entitlements status should be OK (200)")
require.NotNil(ownerEntitlements.JSONDefault, "owner entitlements JSONDefault should not be nil")
require.NotNil(ownerEntitlements.JSONDefault.Values, "owner entitlements value should not be nil")
require.NotNil(tokenEntitlements.JSONDefault, "token entitlements JSONDefault should not be nil")
require.NotNil(tokenEntitlements.JSONDefault.Values, "token entitlements value should not be nil")
// Check if both users have "User admin" entitlement
hasUserAdmin := func(entitlements *[]Entitlement) bool {
for _, entitlement := range *entitlements {
if entitlement.Name != nil && *entitlement.Name == "User admin" {
return true
}
}
return false
}
ownerHasUserAdmin := hasUserAdmin(ownerEntitlements.JSONDefault.Values)
tokenHasUserAdmin := hasUserAdmin(tokenEntitlements.JSONDefault.Values)
t.Logf("Owner has 'User admin': %t", ownerHasUserAdmin)
t.Logf("Token has 'User admin': %t", tokenHasUserAdmin)
// Log all entitlements for debugging
t.Log("Owner entitlements:")
for _, entitlement := range *ownerEntitlements.JSONDefault.Values {
if entitlement.Name != nil {
t.Logf(" - %s", *entitlement.Name)
}
}
t.Log("Token entitlements:")
for _, entitlement := range *tokenEntitlements.JSONDefault.Values {
if entitlement.Name != nil {
t.Logf(" - %s", *entitlement.Name)
}
}
}
func TestBadResponse(t *testing.T) {
require := require.New(t)
baseURL := mustEnv("TRIPLETEX_BASE_URL")
consumerToken := mustEnv("TRIPLETEX_CONSUMER_TOKEN")
employeeToken := mustEnv("TRIPLETEX_EMPLOYEE_TOKEN")
creds := Credentials{
ConsumerToken: consumerToken,
EmployeeToken: employeeToken,
}
c := New(creds, WithBaseURLOption(baseURL))
require.False(c.IsTokenValid(), "token should be invalid after init")
require.NoError(c.CheckAuth(), "should be able to check token after init")
require.True(c.IsTokenValid(), "token should be valid after check")
sorting := "yolo"
from := -69
res, err := c.CompanyWithLoginAccessGetWithLoginAccessWithResponse(context.Background(), &CompanyWithLoginAccessGetWithLoginAccessParams{Sorting: &sorting, From: &from})
require.NoError(err, "should not error for getting companies with login")
require.NotNil(res, "companies with login should not be nil")
require.Equal(&ListResponseClient{}, res.JSONDefault, "zero value should be equal to res.JSONDefault")
require.Equal(http.StatusUnprocessableEntity, res.StatusCode(), "status code should be 422")
require.Nil(res.JSONDefault.Values, "JSONDefault.Values should be nil")
}
// Require environment variable. Panics if not found.
func mustEnv(env string) string {
v, ok := os.LookupEnv(env)
if !ok {
panic(fmt.Sprintf("%s is not set", env))
}
return v
}