forked from nscuro/dtrack-client
-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
about_test.go
98 lines (77 loc) · 2.55 KB
/
about_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
package dtrack
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"log"
"testing"
"github.com/stretchr/testify/require"
)
func TestAboutService_Get(t *testing.T) {
client := setUpContainer(t, testContainerOptions{})
about, err := client.About.Get(context.TODO())
require.NoError(t, err)
require.NotNil(t, about)
require.NotEmpty(t, about.Timestamp)
require.NotEmpty(t, about.Version)
require.NotEqual(t, uuid.Nil, about.UUID)
require.NotEqual(t, uuid.Nil, about.SystemUUID)
require.Equal(t, "Dependency-Track", about.Application)
require.NotEmpty(t, about.Framework.Timestamp)
require.NotEmpty(t, about.Framework.Version)
require.NotEqual(t, uuid.Nil, about.Framework.UUID)
require.Equal(t, "Alpine", about.Framework.Name)
}
type testContainerOptions struct {
Version string
APIPermissions []string
}
func setUpContainer(t *testing.T, options testContainerOptions) *Client {
ctx := context.Background()
version := "latest"
if options.Version != "" {
version = options.Version
}
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: fmt.Sprintf("dependencytrack/apiserver:%s", version),
Env: map[string]string{
"JAVA_OPTIONS": "-Xmx1g",
"SYSTEM_REQUIREMENT_CHECK_ENABLED": "false",
},
ExposedPorts: []string{"8080/tcp"},
WaitingFor: wait.ForLog("Dependency-Track is ready"),
},
Started: true,
})
t.Cleanup(func() {
err = container.Terminate(ctx)
if err != nil {
log.Fatalf("failed to terminate container: %v", err)
}
})
require.NoError(t, err)
apiURL, err := container.Endpoint(ctx, "http")
require.NoError(t, err)
client, err := NewClient(apiURL)
require.NoError(t, err)
err = client.User.ForceChangePassword(ctx, "admin", "admin", "test")
require.NoError(t, err)
bearerToken, err := client.User.Login(ctx, "admin", "test")
require.NoError(t, err)
client, err = NewClient(apiURL, WithBearerToken(bearerToken))
require.NoError(t, err)
team, err := client.Team.Create(ctx, Team{Name: "test"})
require.NoError(t, err)
for _, permissionName := range options.APIPermissions {
_, err = client.Permission.AddPermissionToTeam(ctx, Permission{Name: permissionName}, team.UUID)
require.NoError(t, err)
}
apiKey, err := client.Team.GenerateAPIKey(ctx, team.UUID)
require.NoError(t, err)
client, err = NewClient(apiURL, WithAPIKey(apiKey))
require.NoError(t, err)
return client
}