constellation/internal/license/checker_enterprise_test.go
Daniel Weiße 519efe637d
constellation-lib: run license check in Terraform provider and refactor code (#2740)
* Clean up license checker code

Signed-off-by: Daniel Weiße <dw@edgeless.systems>

* Create license check depending on init/upgrade actions

Signed-off-by: Daniel Weiße <dw@edgeless.systems>

* Run license check in Terraform provider

Signed-off-by: Daniel Weiße <dw@edgeless.systems>

* fix license integration test action

Signed-off-by: Daniel Weiße <dw@edgeless.systems>

* Run tests with enterprise tag

Signed-off-by: Daniel Weiße <dw@edgeless.systems>

* Allow b64 encoding for license ID

Signed-off-by: Daniel Weiße <dw@edgeless.systems>

* Update checker_enterprise.go

---------

Signed-off-by: Daniel Weiße <dw@edgeless.systems>
Co-authored-by: Thomas Tendyck <51411342+thomasten@users.noreply.github.com>
2023-12-22 10:16:36 +01:00

97 lines
2.3 KiB
Go

//go:build enterprise
/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
package license
import (
"bytes"
"context"
"io"
"net/http"
"testing"
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
"github.com/stretchr/testify/assert"
)
// roundTripFunc .
type roundTripFunc func(req *http.Request) *http.Response
// RoundTrip .
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req), nil
}
// newTestClient returns *http.Client with Transport replaced to avoid making real calls.
func newTestClient(fn roundTripFunc) *http.Client {
return &http.Client{
Transport: fn,
}
}
func TestQuotaCheck(t *testing.T) {
testCases := map[string]struct {
license string
serverResponse string
serverResponseCode int
serverResponseContent string
wantQuota int
wantError bool
}{
"success": {
license: "0c0a6558-f8af-4063-bf61-92e7ac4cb052",
serverResponse: "{\"quota\":256}",
serverResponseCode: http.StatusOK,
serverResponseContent: "application/json",
wantQuota: 256,
},
"404": {
serverResponseCode: http.StatusNotFound,
wantError: true,
},
"HTML not JSON": {
serverResponseCode: http.StatusOK,
serverResponseContent: "text/html",
wantError: true,
},
"promise JSON but actually HTML": {
serverResponseCode: http.StatusOK,
serverResponse: "<html><head></head></html>",
serverResponseContent: "application/json",
wantError: true,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
client := &Checker{
httpClient: newTestClient(func(req *http.Request) *http.Response {
r := &http.Response{
StatusCode: tc.serverResponseCode,
Body: io.NopCloser(bytes.NewBufferString(tc.serverResponse)),
Header: make(http.Header),
}
r.Header.Set("Content-Type", tc.serverResponseContent)
return r
}),
}
quota, err := client.CheckLicense(context.Background(), cloudprovider.Unknown, Init, tc.license)
if tc.wantError {
assert.Error(err)
return
}
assert.NoError(err)
assert.Equal(tc.wantQuota, quota)
})
}
}