Skip to content

Commit 3bd44e4

Browse files
authored
🌱 Add Identity and Proxy to API tests. (#331)
1 parent 34c8387 commit 3bd44e4

File tree

7 files changed

+298
-3
lines changed

7 files changed

+298
-3
lines changed

Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,15 @@ endif
123123

124124
# Run unit tests.
125125
test:
126-
go test -v ./auth/
126+
go test -count=1 -v ./auth/
127127

128128
# Run Hub REST API tests.
129129
test-api:
130-
HUB_BASE_URL=${HUB_BASE_URL} go test -v ./test/api/...
130+
HUB_BASE_URL=${HUB_BASE_URL} go test -count=1 -v ./test/api/...
131131

132132
# Run Hub API integration tests.
133133
test-integration:
134-
HUB_BASE_URL=${HUB_BASE_URL} go test -v ./test/integration/...
134+
HUB_BASE_URL=${HUB_BASE_URL} go test -count=1 -v ./test/integration/...
135135

136136
# Run Hub test suite.
137137
test-all: test-unit test-api test-integration

test/api/identity/api_test.go

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package identity
2+
3+
import (
4+
"testing"
5+
6+
"github.com/konveyor/tackle2-hub/test/assert"
7+
)
8+
9+
func TestIdentityCRUD(t *testing.T) {
10+
for _, r := range Samples {
11+
t.Run(r.Name, func(t *testing.T) {
12+
// Create.
13+
err := Identity.Create(&r)
14+
if err != nil {
15+
t.Errorf(err.Error())
16+
}
17+
18+
// Get.
19+
got, err := Identity.Get(r.ID)
20+
if err != nil {
21+
t.Errorf(err.Error())
22+
}
23+
if assert.FlatEqual(got, r) {
24+
t.Errorf("Different response error. Got %v, expected %v", got, r)
25+
}
26+
27+
// Update.
28+
r.Name = "Updated " + r.Name
29+
err = Identity.Update(&r)
30+
if err != nil {
31+
t.Errorf(err.Error())
32+
}
33+
34+
got, err = Identity.Get(r.ID)
35+
if err != nil {
36+
t.Errorf(err.Error())
37+
}
38+
if got.Name != r.Name {
39+
t.Errorf("Different response error. Got %s, expected %s", got.Name, r.Name)
40+
}
41+
42+
// Delete.
43+
err = Identity.Delete(r.ID)
44+
if err != nil {
45+
t.Errorf(err.Error())
46+
}
47+
48+
_, err = Identity.Get(r.ID)
49+
if err == nil {
50+
t.Errorf("Resource exits, but should be deleted: %v", r)
51+
}
52+
})
53+
}
54+
}
55+
56+
func TestIdentityList(t *testing.T) {
57+
samples := Samples
58+
59+
for name := range samples {
60+
sample := samples[name]
61+
assert.Must(t, Identity.Create(&sample))
62+
samples[name] = sample
63+
}
64+
65+
got, err := Identity.List()
66+
if err != nil {
67+
t.Errorf(err.Error())
68+
}
69+
if assert.FlatEqual(got, &samples) {
70+
t.Errorf("Different response error. Got %v, expected %v", got, samples)
71+
}
72+
73+
for _, r := range samples {
74+
assert.Must(t, Identity.Delete(r.ID))
75+
}
76+
}

test/api/identity/pkg.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package identity
2+
3+
import (
4+
"github.com/konveyor/tackle2-hub/binding"
5+
"github.com/konveyor/tackle2-hub/test/api/client"
6+
)
7+
8+
var (
9+
RichClient *binding.RichClient
10+
Identity binding.Identity
11+
)
12+
13+
14+
func init() {
15+
// Prepare RichClient and login to Hub API (configured from env variables).
16+
RichClient = client.PrepareRichClient()
17+
18+
// Shortcut for Identity-related RichClient methods.
19+
Identity = RichClient.Identity
20+
}

test/api/identity/samples.go

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package identity
2+
3+
import (
4+
"github.com/konveyor/tackle2-hub/api"
5+
)
6+
7+
// Set of valid resources for tests and reuse.
8+
var (
9+
GitPw = api.Identity{
10+
Kind: "git",
11+
Name: "git-pw",
12+
User: "test-user",
13+
Password: "test-password-123",
14+
}
15+
GitKey = api.Identity{
16+
Kind: "git",
17+
Name: "git-key",
18+
User: "test-user",
19+
Key: /* The key was generated only for test purposes, not used anywhere, gitleaks:allow */ `-----BEGIN OPENSSH PRIVATE KEY-----
20+
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAlwAAAAdzc2gtcn
21+
NhAAAAAwEAAQAAAIEAoDrNL4XSvv5xgPX4ty4nZFn+5nTG9F9Kvx8u+eNN2CdTKjzFUr0N
22+
HYFfiBo98RyZO67plzQVi7hK+ooUTAdt+eU6bgF23VF62Cd8GT/l2AMB73tTedEYVmsOi5
23+
aJwJV3BaLWmgEbg3YqbtJWeYHloWV1myC7ChYFIqgNgq/NikcAAAIAQpLNF0KSzRcAAAAH
24+
c3NoLXJzYQAAAIEAoDrNL4XSvv5xgPX4ty4nZFn+5nTG9F9Kvx8u+eNN2CdTKjzFUr0NHY
25+
FfiBo98RyZO67plzQVi7hK+ooUTAdt+eU6bgF23VF62Cd8GT/l2AMB73tTedEYVmsOi5aJ
26+
wJV3BaLWmgEbg3YqbtJWeYHloWV1myC7ChYFIqgNgq/NikcAAAADAQABAAAAgC9qd0Hy9j
27+
SzE1+4aou6ysl1oGsOb7ntYS9BySMF0wl+SawcI8CiVXk/ycd7DI2HiwkQb+vg6+dW+ElA
28+
B1WgdTigUZjmhJ8nAFgebav8PbTDc2izFGu31jLeP6dyLRcRG/ztcOvsglloh9Oj//jKzb
29+
YBpKlhIaJbnmVokx0NgAMxAAAAQQCc8TsykYsJQz8R2MpddptTlIStFRIvt7bVcwZ8Hrr5
30+
M8XT3aCxABvtcRgY++/VDZBkK+TNfcdWSS+1Cxrmp/X2AAAAQQDT109BFaf9bbmOdipdyd
31+
nXFhQgxwZ2HbCBtMmOR2zEosCPDNf2c7McFTAOCM3Mh0qNlZPPbUL1v2TVWvDi9NQjAAAA
32+
QQDBoU1fSYFq+Rf2Dyrq9RevXT4VO+uahlje46zV2sVA6Mg2lc+yqTbTIv9NvxnJMC51vO
33+
B1q2YQPNuJWgd03jGNAAAACm1hdWZhcnRAbmI=
34+
-----END OPENSSH PRIVATE KEY-----
35+
`,
36+
}
37+
Mvn = api.Identity{
38+
Kind: "mvn",
39+
Name: "mvn-settings",
40+
Settings: `<?xml version="1.0" encoding="UTF-8"?>
41+
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
42+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
43+
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 http://maven.apache.org/xsd/settings-1.2.0.xsd">
44+
<pluginGroups>
45+
</pluginGroups>
46+
<proxies>
47+
</proxies>
48+
<servers>
49+
<server>
50+
<id>tackle-testapp</id>
51+
<username>GITHUB_USER</username>
52+
<password>GITHUB_TOKEN</password>
53+
</server>
54+
</servers>
55+
<mirrors>
56+
<mirror>
57+
<id>maven-default-http-blocker</id>
58+
<mirrorOf>external:http:*</mirrorOf>
59+
<name>Pseudo repository to mirror external repositories initially using HTTP.</name>
60+
<url>http://0.0.0.0/</url>
61+
<blocked>true</blocked>
62+
</mirror>
63+
</mirrors>
64+
<profiles>
65+
<profile>
66+
<id>github</id>
67+
<repositories>
68+
<repository>
69+
<id>central</id>
70+
<url>https://repo1.maven.org/maven2</url>
71+
</repository>
72+
<repository>
73+
<id>tackle-testapp</id>
74+
<url>https://maven.pkg.github.com/konveyor/tackle-testapp</url>
75+
<snapshots>
76+
<enabled>true</enabled>
77+
</snapshots>
78+
</repository>
79+
</repositories>
80+
</profile>
81+
</profiles>
82+
<activeProfiles>
83+
<activeProfile>github</activeProfile>
84+
</activeProfiles>
85+
</settings>`,
86+
}
87+
Samples = []api.Identity{GitPw, GitKey, Mvn}
88+
)

test/api/proxy/api_test.go

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package proxy
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestProxyGetUpdate(t *testing.T) {
9+
for _, id := range []uint{1,2} { // Existing proxies in Hub have IDs 1, 2
10+
t.Run(fmt.Sprint(id), func(t *testing.T) {
11+
// Get.
12+
orig, err := Proxy.Get(id)
13+
if err != nil {
14+
t.Errorf(err.Error())
15+
}
16+
17+
// Update.
18+
update := orig
19+
update.Host = "127.0.0.1"
20+
update.Port = 8081
21+
update.Enabled = true
22+
err = Proxy.Update(update)
23+
if err != nil {
24+
t.Errorf(err.Error())
25+
}
26+
27+
updated, err := Proxy.Get(update.ID)
28+
if err != nil {
29+
t.Errorf(err.Error())
30+
}
31+
if updated.Host != update.Host || updated.Port != update.Port || updated.Enabled != update.Enabled {
32+
t.Errorf("Different response error. Got %+v, expected %+v", updated, update)
33+
}
34+
35+
// Update back to original.
36+
err = Proxy.Update(orig)
37+
if err != nil {
38+
t.Fatalf(err.Error())
39+
}
40+
})
41+
}
42+
}
43+
44+
func TestSeedProxyList(t *testing.T) {
45+
got, err := Proxy.List()
46+
if err != nil {
47+
t.Errorf(err.Error())
48+
}
49+
foundHttp, foundHttps := false, false
50+
for _, r := range got {
51+
if r.Kind == "http" {
52+
foundHttp = true
53+
}
54+
if r.Kind == "https" {
55+
foundHttps = true
56+
}
57+
}
58+
if !foundHttp {
59+
t.Errorf("Cannot find HTTP proxy.")
60+
}
61+
if !foundHttps {
62+
t.Errorf("Cannot find HTTPS proxy.")
63+
}
64+
}

test/api/proxy/pkg.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package proxy
2+
3+
import (
4+
"github.com/konveyor/tackle2-hub/binding"
5+
"github.com/konveyor/tackle2-hub/test/api/client"
6+
)
7+
8+
var (
9+
RichClient *binding.RichClient
10+
Proxy binding.Proxy
11+
)
12+
13+
14+
func init() {
15+
// Prepare RichClient and login to Hub API (configured from env variables).
16+
RichClient = client.PrepareRichClient()
17+
18+
// Shortcut for Proxy-related RichClient methods.
19+
Proxy = RichClient.Proxy
20+
}

test/api/proxy/samples.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package proxy
2+
3+
import (
4+
"github.com/konveyor/tackle2-hub/api"
5+
)
6+
7+
// Set of valid resources for tests and reuse.
8+
var (
9+
PublicHTTP = api.Proxy{
10+
Kind: "http",
11+
Host: "http-proxy.local",
12+
Port: 80,
13+
}
14+
PublicHTTPS = api.Proxy{
15+
Kind: "https",
16+
Host: "https-proxy.local",
17+
Port: 443,
18+
Excluded: []string{"excldomain.tld"},
19+
}
20+
//PrivateSquid = api.Proxy{
21+
// Kind: "https",
22+
// Host: "squidprivateproxy.local",
23+
// Port: 3128,
24+
// Identity: ,
25+
//}
26+
Samples = []api.Proxy{PublicHTTP, PublicHTTPS}
27+
)

0 commit comments

Comments
 (0)