Skip to content

Commit 522ab4a

Browse files
authored
Merge pull request #9 from deploymenttheory/dev-jl-testing
Unit/Integration Tests and move to HttpExecutor
2 parents 5620135 + 8562b52 commit 522ab4a

File tree

7 files changed

+270
-11
lines changed

7 files changed

+270
-11
lines changed

jamf/jamfprointegration/auth_oauth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (a *oauth) getNewToken() error {
9292
a.expiryTime = time.Now().Add(expiresIn)
9393
a.token = oauthResp.AccessToken
9494

95-
a.Sugar.Info("Token obtained successfully", zap.Time("Expiry", a.expiryTime))
95+
a.Sugar.Infow("Token obtained successfully", "expiry", a.expiryTime)
9696
return nil
9797
}
9898

jamf/jamfprointegration/builders.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88
)
99

1010
// BuildWithOAuth is a helper function allowing the full construct of a Jamf Integration using OAuth2
11-
func BuildWithOAuth(jamfBaseDomain string, Sugar *zap.SugaredLogger, bufferPeriod time.Duration, clientId string, clientSecret string, hideSensitiveData bool, executor httpclient.HTTPExecutor) (*Integration, error) {
11+
func BuildWithOAuth(jamfProFQDN string, Sugar *zap.SugaredLogger, bufferPeriod time.Duration, clientId string, clientSecret string, hideSensitiveData bool, executor httpclient.HTTPExecutor) (*Integration, error) {
1212
integration := Integration{
13-
BaseDomain: jamfBaseDomain,
13+
JamfProFQDN: jamfProFQDN,
1414
Sugar: Sugar,
1515
AuthMethodDescriptor: "oauth2",
1616
httpExecutor: executor,
@@ -23,10 +23,10 @@ func BuildWithOAuth(jamfBaseDomain string, Sugar *zap.SugaredLogger, bufferPerio
2323
}
2424

2525
// BuildWithBasicAuth is a helper function allowing the full construct of a Jamf Integration using BasicAuth
26-
func BuildWithBasicAuth(jamfBaseDomain string, Sugar *zap.SugaredLogger, bufferPeriod time.Duration, username string, password string, hideSensitiveData bool, executor httpclient.HTTPExecutor) (*Integration, error) {
26+
func BuildWithBasicAuth(jamfProFQDN string, Sugar *zap.SugaredLogger, bufferPeriod time.Duration, username string, password string, hideSensitiveData bool, executor httpclient.HTTPExecutor) (*Integration, error) {
2727

2828
integration := Integration{
29-
BaseDomain: jamfBaseDomain,
29+
JamfProFQDN: jamfProFQDN,
3030
Sugar: Sugar,
3131
AuthMethodDescriptor: "basic",
3232
httpExecutor: executor,
@@ -44,7 +44,7 @@ func (j *Integration) BuildOAuth(clientId string, clientSecret string, bufferPer
4444
clientId: clientId,
4545
clientSecret: clientSecret,
4646
bufferPeriod: bufferPeriod,
47-
baseDomain: j.BaseDomain,
47+
baseDomain: j.JamfProFQDN,
4848
Sugar: j.Sugar,
4949
hideSensitiveData: hideSensitiveData,
5050
httpExecutor: executor,
@@ -60,7 +60,7 @@ func (j *Integration) BuildBasicAuth(username string, password string, bufferPer
6060
password: password,
6161
bufferPeriod: bufferPeriod,
6262
Sugar: j.Sugar,
63-
baseDomain: j.BaseDomain,
63+
baseDomain: j.JamfProFQDN,
6464
hideSensitiveData: hideSensitiveData,
6565
httpExecutor: executor,
6666
}

jamf/jamfprointegration/interfaces.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
// JamfAPIHandler implements the APIHandler interface for the Jamf Pro API.
1111
type Integration struct {
12-
BaseDomain string
12+
JamfProFQDN string
1313
AuthMethodDescriptor string
1414
Sugar *zap.SugaredLogger
1515
auth authInterface
@@ -18,7 +18,7 @@ type Integration struct {
1818

1919
// getFQDN returns just the FQDN // TODO remove the "get"
2020
func (j *Integration) GetFQDN() string {
21-
return j.BaseDomain
21+
return j.JamfProFQDN
2222
}
2323

2424
// constructURL appends any endpoint to the FQDN

jamf/jamfprointegration/marshall_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
func TestIntegration_marshalRequest(t *testing.T) {
1212
type fields struct {
13-
BaseDomain string
13+
jamfProFQDN string
1414
AuthMethodDescriptor string
1515
Sugar *zap.SugaredLogger
1616
auth authInterface
@@ -33,7 +33,7 @@ func TestIntegration_marshalRequest(t *testing.T) {
3333
for _, tt := range tests {
3434
t.Run(tt.name, func(t *testing.T) {
3535
j := &Integration{
36-
BaseDomain: tt.fields.BaseDomain,
36+
JamfProFQDN: tt.fields.jamfProFQDN,
3737
AuthMethodDescriptor: tt.fields.AuthMethodDescriptor,
3838
Sugar: tt.fields.Sugar,
3939
auth: tt.fields.auth,
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
package test
2+
3+
import (
4+
"net/http"
5+
"os"
6+
"testing"
7+
"time"
8+
9+
"github.com/deploymenttheory/go-api-http-client-integrations/jamf/jamfprointegration"
10+
"github.com/deploymenttheory/go-api-http-client/httpclient"
11+
"go.uber.org/zap"
12+
)
13+
14+
// API integration has a settable expiry period and is set to 60s
15+
// Account tokens do not have this. I think the expiry is an hour.
16+
17+
func Test_BuildWithOAuth(t *testing.T) {
18+
logger := NewSugaredDevelopmentLogger()
19+
type args struct {
20+
jamfProFQDN string
21+
Sugar *zap.SugaredLogger
22+
bufferPeriod time.Duration
23+
clientId string
24+
clientSecret string
25+
hideSensitiveData bool
26+
executor httpclient.HTTPExecutor
27+
}
28+
tests := []struct {
29+
name string
30+
args args
31+
want *jamfprointegration.Integration
32+
wantErr bool
33+
}{
34+
{
35+
name: "all vars set correctly",
36+
args: args{
37+
jamfProFQDN: os.Getenv(ENV_KEY_JAMFPRO_FQDN),
38+
clientId: os.Getenv(ENV_KEY_CLIENT_ID),
39+
clientSecret: os.Getenv(ENV_KEY_CLIENT_SECRET),
40+
bufferPeriod: 10 * time.Second,
41+
hideSensitiveData: true,
42+
executor: &httpclient.ProdExecutor{Client: &http.Client{}},
43+
Sugar: logger,
44+
},
45+
wantErr: false,
46+
},
47+
{
48+
name: "buffer period too long",
49+
args: args{
50+
jamfProFQDN: os.Getenv(ENV_KEY_JAMFPRO_FQDN),
51+
clientId: os.Getenv(ENV_KEY_CLIENT_ID),
52+
clientSecret: os.Getenv(ENV_KEY_CLIENT_SECRET),
53+
bufferPeriod: 10 * time.Minute,
54+
hideSensitiveData: true,
55+
executor: &httpclient.ProdExecutor{Client: &http.Client{}},
56+
Sugar: logger,
57+
},
58+
wantErr: true,
59+
},
60+
{
61+
name: "no client id",
62+
args: args{
63+
jamfProFQDN: os.Getenv(ENV_KEY_JAMFPRO_FQDN),
64+
clientId: "",
65+
clientSecret: os.Getenv(ENV_KEY_CLIENT_SECRET),
66+
bufferPeriod: 10 * time.Minute,
67+
hideSensitiveData: true,
68+
executor: &httpclient.ProdExecutor{Client: &http.Client{}},
69+
Sugar: logger,
70+
},
71+
wantErr: true,
72+
},
73+
{
74+
name: "no client secret",
75+
args: args{
76+
jamfProFQDN: os.Getenv(ENV_KEY_JAMFPRO_FQDN),
77+
clientId: os.Getenv(ENV_KEY_CLIENT_ID),
78+
clientSecret: "",
79+
bufferPeriod: 10 * time.Minute,
80+
hideSensitiveData: true,
81+
executor: &httpclient.ProdExecutor{Client: &http.Client{}},
82+
Sugar: logger,
83+
},
84+
wantErr: true,
85+
},
86+
}
87+
for _, tt := range tests {
88+
t.Run(tt.name, func(t *testing.T) {
89+
_, err := jamfprointegration.BuildWithOAuth(tt.args.jamfProFQDN, tt.args.Sugar, tt.args.bufferPeriod, tt.args.clientId, tt.args.clientSecret, tt.args.hideSensitiveData, tt.args.executor)
90+
if (err != nil) != tt.wantErr {
91+
t.Errorf("BuildWithOAuth() error = %v, wantErr %v", err, tt.wantErr)
92+
return
93+
}
94+
// Only testing for error as cannot predict pointers inside Integration output. Error is enough to deem success.
95+
})
96+
}
97+
}
98+
99+
func TestBuildWithBasicAuth(t *testing.T) {
100+
logger := NewSugaredDevelopmentLogger()
101+
type args struct {
102+
jamfProFQDN string
103+
Sugar *zap.SugaredLogger
104+
bufferPeriod time.Duration
105+
username string
106+
password string
107+
hideSensitiveData bool
108+
executor httpclient.HTTPExecutor
109+
}
110+
tests := []struct {
111+
name string
112+
args args
113+
want *jamfprointegration.Integration
114+
wantErr bool
115+
}{
116+
{
117+
name: "all vars set correctly",
118+
args: args{
119+
jamfProFQDN: os.Getenv(ENV_KEY_JAMFPRO_FQDN),
120+
username: os.Getenv(ENV_KEY_USERNAME),
121+
password: os.Getenv(ENV_KEY_PASSWORD),
122+
bufferPeriod: 10 * time.Second,
123+
hideSensitiveData: true,
124+
executor: &httpclient.ProdExecutor{Client: &http.Client{}},
125+
Sugar: logger,
126+
},
127+
wantErr: false,
128+
},
129+
{
130+
name: "buffer period too long",
131+
args: args{
132+
jamfProFQDN: os.Getenv(ENV_KEY_JAMFPRO_FQDN),
133+
username: os.Getenv(ENV_KEY_USERNAME),
134+
password: os.Getenv(ENV_KEY_PASSWORD),
135+
bufferPeriod: 100 * time.Minute,
136+
hideSensitiveData: true,
137+
executor: &httpclient.ProdExecutor{Client: &http.Client{}},
138+
Sugar: logger,
139+
},
140+
wantErr: true,
141+
},
142+
{
143+
name: "no username",
144+
args: args{
145+
jamfProFQDN: os.Getenv(ENV_KEY_JAMFPRO_FQDN),
146+
username: "",
147+
password: os.Getenv(ENV_KEY_PASSWORD),
148+
bufferPeriod: 100 * time.Minute,
149+
hideSensitiveData: true,
150+
executor: &httpclient.ProdExecutor{Client: &http.Client{}},
151+
Sugar: logger,
152+
},
153+
wantErr: true,
154+
},
155+
{
156+
name: "no password",
157+
args: args{
158+
jamfProFQDN: os.Getenv(ENV_KEY_JAMFPRO_FQDN),
159+
username: os.Getenv(ENV_KEY_USERNAME),
160+
password: "",
161+
bufferPeriod: 100 * time.Minute,
162+
hideSensitiveData: true,
163+
executor: &httpclient.ProdExecutor{Client: &http.Client{}},
164+
Sugar: logger,
165+
},
166+
wantErr: true,
167+
},
168+
}
169+
for _, tt := range tests {
170+
t.Run(tt.name, func(t *testing.T) {
171+
_, err := jamfprointegration.BuildWithBasicAuth(tt.args.jamfProFQDN, tt.args.Sugar, tt.args.bufferPeriod, tt.args.username, tt.args.password, tt.args.hideSensitiveData, tt.args.executor)
172+
if (err != nil) != tt.wantErr {
173+
t.Errorf("BuildWithBasicAuth() error = %v, wantErr %v", err, tt.wantErr)
174+
return
175+
}
176+
// Only testing for error as cannot predict pointers inside Integration output. Error is enough to deem success.
177+
})
178+
}
179+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package test
2+
3+
import (
4+
"net/http"
5+
"testing"
6+
7+
"github.com/deploymenttheory/go-api-http-client-integrations/jamf/jamfprointegration"
8+
)
9+
10+
func TestIntegration_getSessionCookies(t *testing.T) {
11+
type fields struct {
12+
Integration *jamfprointegration.Integration
13+
}
14+
tests := []struct {
15+
name string
16+
fields fields
17+
want []*http.Cookie
18+
wantErr bool
19+
}{
20+
{
21+
name: "get session cookies ok",
22+
fields: fields{
23+
Integration: NewIntegrationFromEnv(),
24+
},
25+
wantErr: false,
26+
},
27+
}
28+
for _, tt := range tests {
29+
t.Run(tt.name, func(t *testing.T) {
30+
j := tt.fields.Integration
31+
_, err := j.GetSessionCookies()
32+
if (err != nil) != tt.wantErr {
33+
t.Errorf("Integration.getSessionCookies() error = %v, wantErr %v", err, tt.wantErr)
34+
return
35+
}
36+
})
37+
}
38+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package test
2+
3+
import (
4+
"net/http"
5+
"os"
6+
"time"
7+
8+
"github.com/deploymenttheory/go-api-http-client-integrations/jamf/jamfprointegration"
9+
"github.com/deploymenttheory/go-api-http-client/httpclient"
10+
"go.uber.org/zap"
11+
)
12+
13+
const (
14+
ENV_KEY_JAMFPRO_FQDN = "TEST_JAMFPRO_FQDN"
15+
ENV_KEY_CLIENT_ID = "TEST_JAMFPRO_CLIENT_ID"
16+
ENV_KEY_CLIENT_SECRET = "TEST_JAMFPRO_CLIENT_SECRET"
17+
ENV_KEY_USERNAME = "TEST_JAMFPRO_USERNAME"
18+
ENV_KEY_PASSWORD = "TEST_JAMFPRO_PASSWORD"
19+
)
20+
21+
func NewSugaredDevelopmentLogger() *zap.SugaredLogger {
22+
logger, _ := zap.NewDevelopment()
23+
return logger.Sugar()
24+
}
25+
26+
func NewIntegrationFromEnv() *jamfprointegration.Integration {
27+
integration, err := jamfprointegration.BuildWithOAuth(
28+
os.Getenv(ENV_KEY_JAMFPRO_FQDN),
29+
NewSugaredDevelopmentLogger(),
30+
10*time.Second,
31+
os.Getenv(ENV_KEY_CLIENT_ID),
32+
os.Getenv(ENV_KEY_CLIENT_SECRET),
33+
false,
34+
&httpclient.ProdExecutor{Client: &http.Client{}},
35+
)
36+
37+
if err != nil {
38+
panic("we have a problem")
39+
}
40+
41+
return integration
42+
}

0 commit comments

Comments
 (0)