2023-12-22 04:16:36 -05:00
|
|
|
//go:build enterprise
|
|
|
|
|
2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-08-16 10:06:38 -04:00
|
|
|
package license
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2023-12-22 04:16:36 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
2022-08-16 10:06:38 -04:00
|
|
|
"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.
|
2023-12-22 04:16:36 -05:00
|
|
|
func newTestClient(fn roundTripFunc) *http.Client {
|
|
|
|
return &http.Client{
|
|
|
|
Transport: fn,
|
2022-08-16 10:06:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-25 08:06:29 -04:00
|
|
|
func TestQuotaCheck(t *testing.T) {
|
2022-08-16 10:06:38 -04:00
|
|
|
testCases := map[string]struct {
|
|
|
|
license string
|
|
|
|
serverResponse string
|
|
|
|
serverResponseCode int
|
|
|
|
serverResponseContent string
|
|
|
|
wantQuota int
|
|
|
|
wantError bool
|
|
|
|
}{
|
|
|
|
"success": {
|
2022-08-25 08:06:29 -04:00
|
|
|
license: "0c0a6558-f8af-4063-bf61-92e7ac4cb052",
|
2022-08-16 10:06:38 -04:00
|
|
|
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)
|
|
|
|
|
2023-12-22 04:16:36 -05:00
|
|
|
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
|
|
|
|
}),
|
|
|
|
}
|
2022-08-16 10:06:38 -04:00
|
|
|
|
2023-12-22 04:16:36 -05:00
|
|
|
quota, err := client.CheckLicense(context.Background(), cloudprovider.Unknown, Init, tc.license)
|
2022-08-16 10:06:38 -04:00
|
|
|
|
|
|
|
if tc.wantError {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NoError(err)
|
2023-12-22 04:16:36 -05:00
|
|
|
assert.Equal(tc.wantQuota, quota)
|
2022-08-16 10:06:38 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|