2020-02-07 17:24:58 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package admin_test
|
|
|
|
|
|
|
|
import (
|
2020-05-11 17:05:36 +01:00
|
|
|
"encoding/json"
|
2020-04-06 19:29:32 +01:00
|
|
|
"fmt"
|
2022-10-11 12:39:08 +01:00
|
|
|
"io"
|
2020-02-07 17:24:58 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2020-07-06 21:15:55 +01:00
|
|
|
"time"
|
2020-02-07 17:24:58 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2020-05-18 18:36:09 +01:00
|
|
|
"storj.io/common/macaroon"
|
2022-02-04 17:31:24 +00:00
|
|
|
"storj.io/common/memory"
|
2020-05-18 18:36:09 +01:00
|
|
|
"storj.io/common/storj"
|
2020-02-07 17:24:58 +00:00
|
|
|
"storj.io/common/testcontext"
|
2020-05-11 17:05:36 +01:00
|
|
|
"storj.io/common/uuid"
|
2020-02-07 17:24:58 +00:00
|
|
|
"storj.io/storj/private/testplanet"
|
|
|
|
"storj.io/storj/satellite"
|
2020-07-06 21:15:55 +01:00
|
|
|
"storj.io/storj/satellite/accounting"
|
2020-05-18 18:36:09 +01:00
|
|
|
"storj.io/storj/satellite/console"
|
2022-05-27 22:35:13 +01:00
|
|
|
"storj.io/storj/satellite/payments/stripecoinpayments"
|
2020-02-07 17:24:58 +00:00
|
|
|
)
|
|
|
|
|
2021-10-07 11:42:25 +01:00
|
|
|
func TestProjectGet(t *testing.T) {
|
2020-02-07 17:24:58 +00:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
StorageNodeCount: 0,
|
|
|
|
UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
2021-10-01 12:36:41 +01:00
|
|
|
Satellite: func(_ *zap.Logger, _ int, config *satellite.Config) {
|
2020-02-07 17:24:58 +00:00
|
|
|
config.Admin.Address = "127.0.0.1:0"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
2020-05-18 18:36:09 +01:00
|
|
|
sat := planet.Satellites[0]
|
|
|
|
address := sat.Admin.Admin.Listener.Addr()
|
2020-09-05 20:17:18 +01:00
|
|
|
project, err := sat.DB.Console().Projects().Get(ctx, planet.Uplinks[0].Projects[0].ID)
|
|
|
|
require.NoError(t, err)
|
2020-02-07 17:24:58 +00:00
|
|
|
|
2021-10-07 11:42:25 +01:00
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
|
|
link := "http://" + address.String() + "/api/projects/" + project.ID.String()
|
2020-09-05 20:17:18 +01:00
|
|
|
expected := fmt.Sprintf(
|
2023-01-27 21:07:32 +00:00
|
|
|
`{"id":"%s","publicId":"%s","name":"%s","description":"%s","userAgent":null,"ownerId":"%s","rateLimit":null,"burstLimit":null,"maxBuckets":null,"createdAt":"%s","memberCount":0,"storageLimit":"25.00 GB","bandwidthLimit":"25.00 GB","userSpecifiedStorageLimit":null,"userSpecifiedBandwidthLimit":null,"segmentLimit":150000}`,
|
2020-09-05 20:17:18 +01:00
|
|
|
project.ID.String(),
|
2022-06-27 22:20:59 +01:00
|
|
|
project.PublicID.String(),
|
2020-09-05 20:17:18 +01:00
|
|
|
project.Name,
|
|
|
|
project.Description,
|
|
|
|
project.OwnerID.String(),
|
|
|
|
project.CreatedAt.Format(time.RFC3339Nano),
|
|
|
|
)
|
2021-05-14 16:05:42 +01:00
|
|
|
assertGet(ctx, t, link, expected, planet.Satellites[0].Config.Console.AuthToken)
|
2020-09-05 20:17:18 +01:00
|
|
|
})
|
|
|
|
|
2021-10-07 11:42:25 +01:00
|
|
|
t.Run("Not Found", func(t *testing.T) {
|
|
|
|
id, err := uuid.New()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
link := "http://" + address.String() + "/api/projects/" + id.String() + "/limit"
|
|
|
|
body := assertReq(ctx, t, link, http.MethodGet, "", http.StatusNotFound, "", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
require.Contains(t, string(body), "does not exist")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProjectLimit(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
StorageNodeCount: 0,
|
|
|
|
UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: func(_ *zap.Logger, _ int, config *satellite.Config) {
|
|
|
|
config.Admin.Address = "127.0.0.1:0"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
sat := planet.Satellites[0]
|
|
|
|
address := sat.Admin.Admin.Listener.Addr()
|
|
|
|
project, err := sat.DB.Console().Projects().Get(ctx, planet.Uplinks[0].Projects[0].ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
linkLimit := "http://" + address.String() + "/api/projects/" + project.ID.String() + "/limit"
|
|
|
|
|
|
|
|
t.Run("Get OK", func(t *testing.T) {
|
2022-01-18 16:08:11 +00:00
|
|
|
assertGet(ctx, t, linkLimit, `{"usage":{"amount":"25.00 GB","bytes":25000000000},"bandwidth":{"amount":"25.00 GB","bytes":25000000000},"rate":{"rps":0},"maxBuckets":0,"maxSegments":150000}`, planet.Satellites[0].Config.Console.AuthToken)
|
2020-02-07 17:24:58 +00:00
|
|
|
})
|
|
|
|
|
2021-10-07 11:42:25 +01:00
|
|
|
t.Run("Get Not Found", func(t *testing.T) {
|
|
|
|
id, err := uuid.New()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
link := "http://" + address.String() + "/api/projects/" + id.String() + "/limit"
|
|
|
|
body := assertReq(ctx, t, link, http.MethodGet, "", http.StatusNotFound, "", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
require.Contains(t, string(body), "does not exist")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Update Not Found", func(t *testing.T) {
|
|
|
|
id, err := uuid.New()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
link := "http://" + address.String() + "/api/projects/" + id.String() + "/limit?usage=100000000"
|
|
|
|
body := assertReq(ctx, t, link, http.MethodPut, "", http.StatusNotFound, "", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
require.Contains(t, string(body), "does not exist")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Update Nothing", func(t *testing.T) {
|
|
|
|
expectedBody := assertReq(ctx, t, linkLimit, http.MethodGet, "", http.StatusOK, "", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, linkLimit, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusOK, response.StatusCode)
|
|
|
|
require.NoError(t, response.Body.Close())
|
|
|
|
|
|
|
|
assertGet(ctx, t, linkLimit, string(expectedBody), planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Update Usage", func(t *testing.T) {
|
2020-02-07 17:24:58 +00:00
|
|
|
data := url.Values{"usage": []string{"1TiB"}}
|
2021-05-14 16:05:42 +01:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, linkLimit, strings.NewReader(data.Encode()))
|
2020-02-07 17:24:58 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-13 13:40:05 +01:00
|
|
|
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken)
|
2020-02-07 17:24:58 +00:00
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusOK, response.StatusCode)
|
|
|
|
require.NoError(t, response.Body.Close())
|
|
|
|
|
2022-01-18 16:08:11 +00:00
|
|
|
assertGet(ctx, t, linkLimit, `{"usage":{"amount":"1.0 TiB","bytes":1099511627776},"bandwidth":{"amount":"25.00 GB","bytes":25000000000},"rate":{"rps":0},"maxBuckets":0,"maxSegments":150000}`, planet.Satellites[0].Config.Console.AuthToken)
|
2020-02-07 17:24:58 +00:00
|
|
|
|
2021-05-14 16:05:42 +01:00
|
|
|
req, err = http.NewRequestWithContext(ctx, http.MethodPut, linkLimit+"?usage=1GB", nil)
|
2020-02-07 17:24:58 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-13 13:40:05 +01:00
|
|
|
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken)
|
2020-02-07 17:24:58 +00:00
|
|
|
|
|
|
|
response, err = http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusOK, response.StatusCode)
|
|
|
|
require.NoError(t, response.Body.Close())
|
|
|
|
|
2022-01-18 16:08:11 +00:00
|
|
|
assertGet(ctx, t, linkLimit, `{"usage":{"amount":"1.00 GB","bytes":1000000000},"bandwidth":{"amount":"25.00 GB","bytes":25000000000},"rate":{"rps":0},"maxBuckets":0,"maxSegments":150000}`, planet.Satellites[0].Config.Console.AuthToken)
|
2020-05-12 14:01:15 +01:00
|
|
|
})
|
|
|
|
|
2021-10-07 11:42:25 +01:00
|
|
|
t.Run("Update Bandwidth", func(t *testing.T) {
|
2021-05-14 16:05:42 +01:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPut, linkLimit+"?bandwidth=1MB", nil)
|
2020-05-12 14:01:15 +01:00
|
|
|
require.NoError(t, err)
|
2020-08-13 13:40:05 +01:00
|
|
|
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken)
|
2020-05-12 14:01:15 +01:00
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusOK, response.StatusCode)
|
|
|
|
require.NoError(t, response.Body.Close())
|
|
|
|
|
2022-01-18 16:08:11 +00:00
|
|
|
assertGet(ctx, t, linkLimit, `{"usage":{"amount":"1.00 GB","bytes":1000000000},"bandwidth":{"amount":"1.00 MB","bytes":1000000},"rate":{"rps":0},"maxBuckets":0,"maxSegments":150000}`, planet.Satellites[0].Config.Console.AuthToken)
|
2020-02-07 17:24:58 +00:00
|
|
|
})
|
|
|
|
|
2021-10-07 11:42:25 +01:00
|
|
|
t.Run("Update Rate", func(t *testing.T) {
|
2021-05-14 16:05:42 +01:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPut, linkLimit+"?rate=100", nil)
|
2020-02-07 17:24:58 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-13 13:40:05 +01:00
|
|
|
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken)
|
2020-02-07 17:24:58 +00:00
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusOK, response.StatusCode)
|
|
|
|
require.NoError(t, response.Body.Close())
|
|
|
|
|
2022-01-18 16:08:11 +00:00
|
|
|
assertGet(ctx, t, linkLimit, `{"usage":{"amount":"1.00 GB","bytes":1000000000},"bandwidth":{"amount":"1.00 MB","bytes":1000000},"rate":{"rps":100},"maxBuckets":0,"maxSegments":150000}`, planet.Satellites[0].Config.Console.AuthToken)
|
2020-07-15 15:14:56 +01:00
|
|
|
})
|
2021-10-07 11:42:25 +01:00
|
|
|
|
|
|
|
t.Run("Update Buckets", func(t *testing.T) {
|
2021-05-14 16:05:42 +01:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPut, linkLimit+"?buckets=2000", nil)
|
2020-07-15 15:14:56 +01:00
|
|
|
require.NoError(t, err)
|
2020-08-13 13:40:05 +01:00
|
|
|
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken)
|
2020-07-15 15:14:56 +01:00
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusOK, response.StatusCode)
|
|
|
|
require.NoError(t, response.Body.Close())
|
|
|
|
|
2022-01-18 16:08:11 +00:00
|
|
|
assertGet(ctx, t, linkLimit, `{"usage":{"amount":"1.00 GB","bytes":1000000000},"bandwidth":{"amount":"1.00 MB","bytes":1000000},"rate":{"rps":100},"maxBuckets":2000,"maxSegments":150000}`, planet.Satellites[0].Config.Console.AuthToken)
|
2021-12-08 13:18:40 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Update Segment Limit", func(t *testing.T) {
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPut, linkLimit+"?segments=500", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusOK, response.StatusCode)
|
|
|
|
require.NoError(t, response.Body.Close())
|
|
|
|
|
|
|
|
assertGet(ctx, t, linkLimit, `{"usage":{"amount":"1.00 GB","bytes":1000000000},"bandwidth":{"amount":"1.00 MB","bytes":1000000},"rate":{"rps":100},"maxBuckets":2000,"maxSegments":500}`, planet.Satellites[0].Config.Console.AuthToken)
|
2020-02-07 17:24:58 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-07 11:42:25 +01:00
|
|
|
func TestProjectAdd(t *testing.T) {
|
2020-05-11 17:05:36 +01:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
StorageNodeCount: 0,
|
|
|
|
UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
2021-10-01 12:36:41 +01:00
|
|
|
Satellite: func(_ *zap.Logger, _ int, config *satellite.Config) {
|
2020-05-11 17:05:36 +01:00
|
|
|
config.Admin.Address = "127.0.0.1:0"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
address := planet.Satellites[0].Admin.Admin.Listener.Addr()
|
|
|
|
userID := planet.Uplinks[0].Projects[0].Owner
|
|
|
|
|
|
|
|
body := strings.NewReader(fmt.Sprintf(`{"ownerId":"%s","projectName":"Test Project"}`, userID.ID.String()))
|
2021-08-03 12:24:21 +01:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://"+address.String()+"/api/projects", body)
|
2020-05-11 17:05:36 +01:00
|
|
|
require.NoError(t, err)
|
2020-08-13 13:40:05 +01:00
|
|
|
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken)
|
2020-05-11 17:05:36 +01:00
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusOK, response.StatusCode)
|
2021-10-01 12:36:41 +01:00
|
|
|
require.Equal(t, "application/json", response.Header.Get("Content-Type"))
|
2022-10-11 12:39:08 +01:00
|
|
|
responseBody, err := io.ReadAll(response.Body)
|
2020-05-11 17:05:36 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, response.Body.Close())
|
|
|
|
|
|
|
|
var output struct {
|
|
|
|
ProjectID uuid.UUID `json:"projectId"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(responseBody, &output)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
project, err := planet.Satellites[0].DB.Console().Projects().Get(ctx, output.ProjectID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "Test Project", project.Name)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-07 11:42:25 +01:00
|
|
|
func TestProjectRename(t *testing.T) {
|
2020-07-06 21:28:49 +01:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
StorageNodeCount: 0,
|
|
|
|
UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
2021-10-01 12:36:41 +01:00
|
|
|
Satellite: func(_ *zap.Logger, _ int, config *satellite.Config) {
|
2020-07-06 21:28:49 +01:00
|
|
|
config.Admin.Address = "127.0.0.1:0"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
address := planet.Satellites[0].Admin.Admin.Listener.Addr()
|
|
|
|
userID := planet.Uplinks[0].Projects[0].Owner
|
|
|
|
oldName, newName := "renameTest", "Test Project"
|
|
|
|
|
|
|
|
project, err := planet.Satellites[0].AddProject(ctx, userID.ID, oldName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, oldName, project.Name)
|
|
|
|
|
2021-10-07 11:42:25 +01:00
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
|
|
body := strings.NewReader(fmt.Sprintf(`{"projectName":"%s","description":"This project got renamed"}`, newName))
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPut, fmt.Sprintf("http://"+address.String()+"/api/projects/%s", project.ID.String()), body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken)
|
2020-07-06 21:28:49 +01:00
|
|
|
|
2021-10-07 11:42:25 +01:00
|
|
|
response, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusOK, response.StatusCode)
|
|
|
|
require.Equal(t, "", response.Header.Get("Content-Type"))
|
|
|
|
require.NoError(t, response.Body.Close())
|
2020-07-06 21:28:49 +01:00
|
|
|
|
2021-10-07 11:42:25 +01:00
|
|
|
project, err = planet.Satellites[0].DB.Console().Projects().Get(ctx, project.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, newName, project.Name)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Not Found", func(t *testing.T) {
|
|
|
|
id, err := uuid.New()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
link := fmt.Sprintf("http://"+address.String()+"/api/projects/%s", id.String())
|
|
|
|
putBody := fmt.Sprintf(`{"projectName":"%s","description":"This project got renamed"}`, newName)
|
|
|
|
body := assertReq(ctx, t, link, http.MethodPut, putBody, http.StatusNotFound, "", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
require.Contains(t, string(body), "does not exist")
|
|
|
|
})
|
2020-07-06 21:28:49 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-07 11:42:25 +01:00
|
|
|
func TestProjectDelete(t *testing.T) {
|
2020-05-18 18:36:09 +01:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
StorageNodeCount: 0,
|
|
|
|
UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
2021-10-01 12:36:41 +01:00
|
|
|
Satellite: func(_ *zap.Logger, _ int, config *satellite.Config) {
|
2020-05-18 18:36:09 +01:00
|
|
|
config.Admin.Address = "127.0.0.1:0"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
address := planet.Satellites[0].Admin.Admin.Listener.Addr()
|
|
|
|
projectID := planet.Uplinks[0].Projects[0].ID
|
|
|
|
|
|
|
|
// Ensure there are no buckets left
|
2021-11-12 20:47:41 +00:00
|
|
|
buckets, err := planet.Satellites[0].API.Buckets.Service.ListBuckets(ctx, projectID, storj.BucketListOptions{Limit: 1, Direction: storj.Forward}, macaroon.AllowedBuckets{All: true})
|
2020-05-18 18:36:09 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, buckets.Items, 0)
|
|
|
|
|
|
|
|
apikeys, err := planet.Satellites[0].DB.Console().APIKeys().GetPagedByProjectID(ctx, projectID, console.APIKeyCursor{
|
|
|
|
Page: 1,
|
|
|
|
Limit: 2,
|
|
|
|
Search: "",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, apikeys.APIKeys, 1)
|
|
|
|
|
|
|
|
// the deletion with an existing API key should fail
|
2021-08-03 12:24:21 +01:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, fmt.Sprintf("http://"+address.String()+"/api/projects/%s", projectID), nil)
|
2020-05-18 18:36:09 +01:00
|
|
|
require.NoError(t, err)
|
2020-08-13 13:40:05 +01:00
|
|
|
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken)
|
2020-05-18 18:36:09 +01:00
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, response.Body.Close())
|
|
|
|
require.Equal(t, http.StatusConflict, response.StatusCode)
|
2021-10-01 12:36:41 +01:00
|
|
|
require.Equal(t, "application/json", response.Header.Get("Content-Type"))
|
2020-05-18 18:36:09 +01:00
|
|
|
|
|
|
|
err = planet.Satellites[0].DB.Console().APIKeys().Delete(ctx, apikeys.APIKeys[0].ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-08-03 12:24:21 +01:00
|
|
|
req, err = http.NewRequestWithContext(ctx, http.MethodDelete, fmt.Sprintf("http://"+address.String()+"/api/projects/%s", projectID), nil)
|
2020-05-18 18:36:09 +01:00
|
|
|
require.NoError(t, err)
|
2020-08-13 13:40:05 +01:00
|
|
|
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken)
|
2020-05-18 18:36:09 +01:00
|
|
|
|
|
|
|
response, err = http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, response.Body.Close())
|
|
|
|
require.Equal(t, http.StatusOK, response.StatusCode)
|
2021-10-01 12:36:41 +01:00
|
|
|
require.Equal(t, "", response.Header.Get("Content-Type"))
|
2020-05-18 18:36:09 +01:00
|
|
|
|
|
|
|
project, err := planet.Satellites[0].DB.Console().Projects().Get(ctx, projectID)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, project)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-07 11:42:25 +01:00
|
|
|
func TestProjectCheckUsage_withoutUsage(t *testing.T) {
|
2020-08-13 13:40:05 +01:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
StorageNodeCount: 0,
|
|
|
|
UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
2021-10-01 12:36:41 +01:00
|
|
|
Satellite: func(_ *zap.Logger, _ int, config *satellite.Config) {
|
2020-08-13 13:40:05 +01:00
|
|
|
config.Admin.Address = "127.0.0.1:0"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
address := planet.Satellites[0].Admin.Admin.Listener.Addr()
|
|
|
|
projectID := planet.Uplinks[0].Projects[0].ID
|
|
|
|
|
|
|
|
apiKeys, err := planet.Satellites[0].DB.Console().APIKeys().GetPagedByProjectID(ctx, projectID, console.APIKeyCursor{
|
|
|
|
Page: 1,
|
|
|
|
Limit: 2,
|
|
|
|
Search: "",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, apiKeys.APIKeys, 1)
|
|
|
|
|
|
|
|
err = planet.Satellites[0].DB.Console().APIKeys().Delete(ctx, apiKeys.APIKeys[0].ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-08-03 12:24:21 +01:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("http://"+address.String()+"/api/projects/%s/usage", projectID), nil)
|
2020-08-13 13:40:05 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
2021-10-01 12:36:41 +01:00
|
|
|
require.Equal(t, http.StatusOK, response.StatusCode)
|
|
|
|
require.Equal(t, "application/json", response.Header.Get("Content-Type"))
|
2022-10-11 12:39:08 +01:00
|
|
|
responseBody, err := io.ReadAll(response.Body)
|
2020-08-13 13:40:05 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "{\"result\":\"no project usage exist\"}", string(responseBody))
|
|
|
|
require.NoError(t, response.Body.Close())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-07 11:42:25 +01:00
|
|
|
func TestProjectCheckUsage_withUsage(t *testing.T) {
|
2020-07-06 21:15:55 +01:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
StorageNodeCount: 0,
|
|
|
|
UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
2021-10-01 12:36:41 +01:00
|
|
|
Satellite: func(_ *zap.Logger, _ int, config *satellite.Config) {
|
2020-07-06 21:15:55 +01:00
|
|
|
config.Admin.Address = "127.0.0.1:0"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
address := planet.Satellites[0].Admin.Admin.Listener.Addr()
|
|
|
|
projectID := planet.Uplinks[0].Projects[0].ID
|
|
|
|
|
|
|
|
apiKeys, err := planet.Satellites[0].DB.Console().APIKeys().GetPagedByProjectID(ctx, projectID, console.APIKeyCursor{
|
|
|
|
Page: 1,
|
|
|
|
Limit: 2,
|
|
|
|
Search: "",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, apiKeys.APIKeys, 1)
|
|
|
|
|
|
|
|
err = planet.Satellites[0].DB.Console().APIKeys().Delete(ctx, apiKeys.APIKeys[0].ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-09-01 15:03:52 +01:00
|
|
|
now := time.Now().UTC()
|
|
|
|
// use fixed intervals to avoid issues at the beginning of the month
|
2020-07-06 21:15:55 +01:00
|
|
|
tally := accounting.BucketStorageTally{
|
2021-07-01 12:29:25 +01:00
|
|
|
BucketName: "test",
|
|
|
|
ProjectID: projectID,
|
|
|
|
IntervalStart: time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 1, time.UTC),
|
|
|
|
ObjectCount: 1,
|
|
|
|
TotalSegmentCount: 2,
|
|
|
|
TotalBytes: 640000,
|
|
|
|
MetadataSize: 2,
|
2020-07-06 21:15:55 +01:00
|
|
|
}
|
|
|
|
err = planet.Satellites[0].DB.ProjectAccounting().CreateStorageTally(ctx, tally)
|
|
|
|
require.NoError(t, err)
|
|
|
|
tally = accounting.BucketStorageTally{
|
2021-07-01 12:29:25 +01:00
|
|
|
BucketName: "test",
|
|
|
|
ProjectID: projectID,
|
|
|
|
IntervalStart: time.Date(now.Year(), now.Month(), 1, 0, 1, 0, 1, time.UTC),
|
|
|
|
ObjectCount: 1,
|
|
|
|
TotalSegmentCount: 2,
|
|
|
|
TotalBytes: 640000,
|
|
|
|
MetadataSize: 2,
|
2020-07-06 21:15:55 +01:00
|
|
|
}
|
|
|
|
err = planet.Satellites[0].DB.ProjectAccounting().CreateStorageTally(ctx, tally)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-02-04 17:31:24 +00:00
|
|
|
// Ensure User is free tier.
|
|
|
|
paid, err := planet.Satellites[0].DB.Console().Users().GetUserPaidTier(ctx, planet.Uplinks[0].Projects[0].Owner.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, paid)
|
|
|
|
|
2021-08-03 12:24:21 +01:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("http://"+address.String()+"/api/projects/%s/usage", projectID), nil)
|
2020-07-06 21:15:55 +01:00
|
|
|
require.NoError(t, err)
|
2020-08-13 13:40:05 +01:00
|
|
|
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken)
|
2020-07-06 21:15:55 +01:00
|
|
|
|
2020-08-13 13:40:05 +01:00
|
|
|
response, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
2022-02-04 17:31:24 +00:00
|
|
|
require.Equal(t, http.StatusOK, response.StatusCode)
|
2021-10-01 12:36:41 +01:00
|
|
|
require.Equal(t, "application/json", response.Header.Get("Content-Type"))
|
|
|
|
|
2022-10-11 12:39:08 +01:00
|
|
|
responseBody, err := io.ReadAll(response.Body)
|
2020-07-06 21:15:55 +01:00
|
|
|
require.NoError(t, err)
|
2022-02-04 17:31:24 +00:00
|
|
|
require.Equal(t, "{\"result\":\"no project usage exist\"}", string(responseBody))
|
|
|
|
require.NoError(t, response.Body.Close())
|
|
|
|
|
|
|
|
// Make User paid tier.
|
2022-02-28 21:37:46 +00:00
|
|
|
err = planet.Satellites[0].DB.Console().Users().UpdatePaidTier(ctx, planet.Uplinks[0].Projects[0].Owner.ID, true, memory.PB, memory.PB, 1000000, 3)
|
2022-02-04 17:31:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Ensure User is paid tier.
|
|
|
|
paid, err = planet.Satellites[0].DB.Console().Users().GetUserPaidTier(ctx, planet.Uplinks[0].Projects[0].Owner.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, paid)
|
|
|
|
|
|
|
|
req, err = http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("http://"+address.String()+"/api/projects/%s/usage", projectID), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
|
|
|
|
response, err = http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusConflict, response.StatusCode)
|
|
|
|
require.Equal(t, "application/json", response.Header.Get("Content-Type"))
|
|
|
|
|
2022-10-11 12:39:08 +01:00
|
|
|
responseBody, err = io.ReadAll(response.Body)
|
2022-02-04 17:31:24 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-13 13:40:05 +01:00
|
|
|
require.Equal(t, "{\"error\":\"usage for current month exists\",\"detail\":\"\"}", string(responseBody))
|
|
|
|
require.NoError(t, response.Body.Close())
|
|
|
|
})
|
|
|
|
}
|
2020-07-06 21:15:55 +01:00
|
|
|
|
2021-10-07 11:42:25 +01:00
|
|
|
func TestProjectCheckUsage_lastMonthUnappliedInvoice(t *testing.T) {
|
2020-09-03 22:12:26 +01:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
StorageNodeCount: 0,
|
|
|
|
UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
2021-10-01 12:36:41 +01:00
|
|
|
Satellite: func(_ *zap.Logger, _ int, config *satellite.Config) {
|
2020-09-03 22:12:26 +01:00
|
|
|
config.Admin.Address = "127.0.0.1:0"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
address := planet.Satellites[0].Admin.Admin.Listener.Addr()
|
|
|
|
projectID := planet.Uplinks[0].Projects[0].ID
|
|
|
|
|
|
|
|
apiKeys, err := planet.Satellites[0].DB.Console().APIKeys().GetPagedByProjectID(ctx, projectID, console.APIKeyCursor{
|
|
|
|
Page: 1,
|
|
|
|
Limit: 2,
|
|
|
|
Search: "",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, apiKeys.APIKeys, 1)
|
|
|
|
|
|
|
|
err = planet.Satellites[0].DB.Console().APIKeys().Delete(ctx, apiKeys.APIKeys[0].ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-09-30 05:10:02 +01:00
|
|
|
now := time.Date(2030, time.Month(9), 1, 0, 0, 0, 0, time.UTC)
|
2020-09-03 22:12:26 +01:00
|
|
|
|
|
|
|
oneMonthAhead := now.AddDate(0, 1, 0)
|
|
|
|
planet.Satellites[0].Admin.Admin.Server.SetNow(func() time.Time {
|
|
|
|
return oneMonthAhead
|
|
|
|
})
|
|
|
|
|
|
|
|
// use fixed intervals to avoid issues at the beginning of the month
|
|
|
|
tally := accounting.BucketStorageTally{
|
2021-07-01 12:29:25 +01:00
|
|
|
BucketName: "test",
|
|
|
|
ProjectID: projectID,
|
|
|
|
IntervalStart: time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 1, time.UTC),
|
|
|
|
ObjectCount: 1,
|
|
|
|
TotalSegmentCount: 2,
|
|
|
|
TotalBytes: 640000,
|
|
|
|
MetadataSize: 2,
|
2020-09-03 22:12:26 +01:00
|
|
|
}
|
|
|
|
err = planet.Satellites[0].DB.ProjectAccounting().CreateStorageTally(ctx, tally)
|
|
|
|
require.NoError(t, err)
|
|
|
|
tally = accounting.BucketStorageTally{
|
2021-07-01 12:29:25 +01:00
|
|
|
BucketName: "test",
|
|
|
|
ProjectID: projectID,
|
|
|
|
IntervalStart: time.Date(now.Year(), now.Month(), 1, 0, 1, 0, 1, time.UTC),
|
|
|
|
ObjectCount: 1,
|
|
|
|
TotalSegmentCount: 2,
|
|
|
|
TotalBytes: 640000,
|
|
|
|
MetadataSize: 2,
|
2020-09-03 22:12:26 +01:00
|
|
|
}
|
|
|
|
err = planet.Satellites[0].DB.ProjectAccounting().CreateStorageTally(ctx, tally)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-04-28 03:54:56 +01:00
|
|
|
planet.Satellites[0].API.Payments.StripeService.SetNow(func() time.Time {
|
2020-09-03 22:12:26 +01:00
|
|
|
return oneMonthAhead
|
|
|
|
})
|
2022-04-28 03:54:56 +01:00
|
|
|
err = planet.Satellites[0].API.Payments.StripeService.PrepareInvoiceProjectRecords(ctx, now)
|
2020-09-03 22:12:26 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-08-03 12:24:21 +01:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("http://"+address.String()+"/api/projects/%s/usage", projectID), nil)
|
2020-09-03 22:12:26 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
2021-10-01 12:36:41 +01:00
|
|
|
require.Equal(t, http.StatusConflict, response.StatusCode)
|
|
|
|
|
|
|
|
require.Equal(t, "application/json", response.Header.Get("Content-Type"))
|
2022-10-11 12:39:08 +01:00
|
|
|
responseBody, err := io.ReadAll(response.Body)
|
2020-09-03 22:12:26 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "{\"error\":\"unapplied project invoice record exist\",\"detail\":\"\"}", string(responseBody))
|
|
|
|
require.NoError(t, response.Body.Close())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-27 22:35:13 +01:00
|
|
|
// TestProjectDelete_withUsageCurrentMonth first tries to delete an actively used project of a paid tier user, which
|
2022-02-04 17:31:24 +00:00
|
|
|
// should fail and afterwards converts the user to free tier and tries the deletion again. That deletion should succeed.
|
2021-10-07 11:42:25 +01:00
|
|
|
func TestProjectDelete_withUsageCurrentMonth(t *testing.T) {
|
2020-08-13 13:40:05 +01:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
StorageNodeCount: 0,
|
|
|
|
UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
2021-10-01 12:36:41 +01:00
|
|
|
Satellite: func(_ *zap.Logger, _ int, config *satellite.Config) {
|
2020-08-13 13:40:05 +01:00
|
|
|
config.Admin.Address = "127.0.0.1:0"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
address := planet.Satellites[0].Admin.Admin.Listener.Addr()
|
|
|
|
projectID := planet.Uplinks[0].Projects[0].ID
|
|
|
|
|
|
|
|
apiKeys, err := planet.Satellites[0].DB.Console().APIKeys().GetPagedByProjectID(ctx, projectID, console.APIKeyCursor{
|
|
|
|
Page: 1,
|
|
|
|
Limit: 2,
|
|
|
|
Search: "",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, apiKeys.APIKeys, 1)
|
|
|
|
|
|
|
|
err = planet.Satellites[0].DB.Console().APIKeys().Delete(ctx, apiKeys.APIKeys[0].ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-09-01 15:03:52 +01:00
|
|
|
now := time.Now().UTC()
|
|
|
|
// use fixed intervals to avoid issues at the beginning of the month
|
2020-08-13 13:40:05 +01:00
|
|
|
tally := accounting.BucketStorageTally{
|
2021-07-01 12:29:25 +01:00
|
|
|
BucketName: "test",
|
|
|
|
ProjectID: projectID,
|
|
|
|
IntervalStart: time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 1, time.UTC),
|
|
|
|
ObjectCount: 1,
|
|
|
|
TotalSegmentCount: 2,
|
|
|
|
TotalBytes: 640000,
|
|
|
|
MetadataSize: 2,
|
2020-08-13 13:40:05 +01:00
|
|
|
}
|
|
|
|
err = planet.Satellites[0].DB.ProjectAccounting().CreateStorageTally(ctx, tally)
|
|
|
|
require.NoError(t, err)
|
|
|
|
tally = accounting.BucketStorageTally{
|
2021-07-01 12:29:25 +01:00
|
|
|
BucketName: "test",
|
|
|
|
ProjectID: projectID,
|
|
|
|
IntervalStart: time.Date(now.Year(), now.Month(), 1, 0, 1, 0, 1, time.UTC),
|
|
|
|
ObjectCount: 1,
|
|
|
|
TotalSegmentCount: 2,
|
|
|
|
TotalBytes: 640000,
|
|
|
|
MetadataSize: 2,
|
2020-08-13 13:40:05 +01:00
|
|
|
}
|
|
|
|
err = planet.Satellites[0].DB.ProjectAccounting().CreateStorageTally(ctx, tally)
|
2020-07-06 21:15:55 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-02-04 17:31:24 +00:00
|
|
|
// Make User paid tier.
|
2022-02-28 21:37:46 +00:00
|
|
|
err = planet.Satellites[0].DB.Console().Users().UpdatePaidTier(ctx, planet.Uplinks[0].Projects[0].Owner.ID, true, memory.PB, memory.PB, 1000000, 3)
|
2022-02-04 17:31:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Ensure User is paid tier.
|
|
|
|
paid, err := planet.Satellites[0].DB.Console().Users().GetUserPaidTier(ctx, planet.Uplinks[0].Projects[0].Owner.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, paid)
|
|
|
|
|
2021-08-03 12:24:21 +01:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, fmt.Sprintf("http://"+address.String()+"/api/projects/%s", projectID), nil)
|
2020-07-06 21:15:55 +01:00
|
|
|
require.NoError(t, err)
|
2020-08-13 13:40:05 +01:00
|
|
|
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken)
|
2020-07-06 21:15:55 +01:00
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
2021-10-01 12:36:41 +01:00
|
|
|
require.Equal(t, http.StatusConflict, response.StatusCode)
|
|
|
|
require.Equal(t, "application/json", response.Header.Get("Content-Type"))
|
|
|
|
|
2022-10-11 12:39:08 +01:00
|
|
|
responseBody, err := io.ReadAll(response.Body)
|
2020-07-06 21:15:55 +01:00
|
|
|
require.NoError(t, err)
|
2020-08-05 14:13:11 +01:00
|
|
|
require.Equal(t, "{\"error\":\"usage for current month exists\",\"detail\":\"\"}", string(responseBody))
|
2020-07-06 21:15:55 +01:00
|
|
|
require.NoError(t, response.Body.Close())
|
2022-02-04 17:31:24 +00:00
|
|
|
|
|
|
|
// Make User free tier again.
|
2022-02-28 21:37:46 +00:00
|
|
|
err = planet.Satellites[0].DB.Console().Users().UpdatePaidTier(ctx, planet.Uplinks[0].Projects[0].Owner.ID, false, memory.TB, memory.TB, 100000, 3)
|
2022-02-04 17:31:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Ensure User is free tier.
|
|
|
|
paid, err = planet.Satellites[0].DB.Console().Users().GetUserPaidTier(ctx, planet.Uplinks[0].Projects[0].Owner.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, paid)
|
|
|
|
|
|
|
|
req, err = http.NewRequestWithContext(ctx, http.MethodDelete, fmt.Sprintf("http://"+address.String()+"/api/projects/%s", projectID), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
|
|
|
|
response, err = http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusOK, response.StatusCode)
|
|
|
|
|
2022-10-11 12:39:08 +01:00
|
|
|
responseBody, err = io.ReadAll(response.Body)
|
2022-02-04 17:31:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "", string(responseBody))
|
|
|
|
require.NoError(t, response.Body.Close())
|
2020-07-06 21:15:55 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-27 22:35:13 +01:00
|
|
|
// TestProjectDelete_withUsageCurrentMonthUncharged first tries to delete a last month used project of a paid tier user, which
|
2022-02-04 17:31:24 +00:00
|
|
|
// should fail and afterwards converts the user to free tier and tries the deletion again. That deletion should succeed.
|
|
|
|
// This test ensures we bill paid tier users for past months usage and do not forget to do so.
|
2022-05-27 22:35:13 +01:00
|
|
|
func TestProjectDelete_withUsagePreviousMonthUncharged(t *testing.T) {
|
2020-07-06 21:15:55 +01:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
StorageNodeCount: 0,
|
|
|
|
UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
2021-10-01 12:36:41 +01:00
|
|
|
Satellite: func(_ *zap.Logger, _ int, config *satellite.Config) {
|
2020-07-06 21:15:55 +01:00
|
|
|
config.Admin.Address = "127.0.0.1:0"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
address := planet.Satellites[0].Admin.Admin.Listener.Addr()
|
|
|
|
projectID := planet.Uplinks[0].Projects[0].ID
|
|
|
|
|
|
|
|
apiKeys, err := planet.Satellites[0].DB.Console().APIKeys().GetPagedByProjectID(ctx, projectID, console.APIKeyCursor{
|
|
|
|
Page: 1,
|
|
|
|
Limit: 2,
|
|
|
|
Search: "",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, apiKeys.APIKeys, 1)
|
|
|
|
|
|
|
|
err = planet.Satellites[0].DB.Console().APIKeys().Delete(ctx, apiKeys.APIKeys[0].ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-13 13:47:55 +01:00
|
|
|
// TODO: Improve updating of DB entries
|
2020-07-29 13:17:11 +01:00
|
|
|
now := time.Now().UTC()
|
|
|
|
// set fixed day to avoid failures at the end of the month
|
|
|
|
accTime := time.Date(now.Year(), now.Month()-1, 15, now.Hour(), now.Minute(), now.Second(), now.Nanosecond(), time.UTC)
|
2020-07-06 21:15:55 +01:00
|
|
|
tally := accounting.BucketStorageTally{
|
2021-07-01 12:29:25 +01:00
|
|
|
BucketName: "test",
|
|
|
|
ProjectID: projectID,
|
|
|
|
IntervalStart: accTime,
|
|
|
|
ObjectCount: 1,
|
|
|
|
TotalSegmentCount: 2,
|
|
|
|
TotalBytes: 640000,
|
|
|
|
MetadataSize: 2,
|
2020-07-06 21:15:55 +01:00
|
|
|
}
|
|
|
|
err = planet.Satellites[0].DB.ProjectAccounting().CreateStorageTally(ctx, tally)
|
|
|
|
require.NoError(t, err)
|
|
|
|
tally = accounting.BucketStorageTally{
|
2021-07-01 12:29:25 +01:00
|
|
|
BucketName: "test",
|
|
|
|
ProjectID: projectID,
|
|
|
|
IntervalStart: accTime.AddDate(0, 0, 1),
|
|
|
|
ObjectCount: 1,
|
|
|
|
TotalSegmentCount: 2,
|
|
|
|
TotalBytes: 640000,
|
|
|
|
MetadataSize: 2,
|
2020-07-06 21:15:55 +01:00
|
|
|
}
|
|
|
|
err = planet.Satellites[0].DB.ProjectAccounting().CreateStorageTally(ctx, tally)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-02-04 17:31:24 +00:00
|
|
|
// Make User paid tier.
|
2022-02-28 21:37:46 +00:00
|
|
|
err = planet.Satellites[0].DB.Console().Users().UpdatePaidTier(ctx, planet.Uplinks[0].Projects[0].Owner.ID, true, memory.PB, memory.PB, 1000000, 3)
|
2022-02-04 17:31:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Ensure User is paid tier.
|
|
|
|
paid, err := planet.Satellites[0].DB.Console().Users().GetUserPaidTier(ctx, planet.Uplinks[0].Projects[0].Owner.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, paid)
|
|
|
|
|
2021-08-03 12:24:21 +01:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, fmt.Sprintf("http://"+address.String()+"/api/projects/%s", projectID), nil)
|
2020-07-06 21:15:55 +01:00
|
|
|
require.NoError(t, err)
|
2020-08-13 13:40:05 +01:00
|
|
|
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken)
|
2020-07-06 21:15:55 +01:00
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
2022-10-11 12:39:08 +01:00
|
|
|
responseBody, err := io.ReadAll(response.Body)
|
2020-07-06 21:15:55 +01:00
|
|
|
require.NoError(t, err)
|
2020-08-05 14:13:11 +01:00
|
|
|
require.Equal(t, "{\"error\":\"usage for last month exist, but is not billed yet\",\"detail\":\"\"}", string(responseBody))
|
2020-07-06 21:15:55 +01:00
|
|
|
require.NoError(t, response.Body.Close())
|
|
|
|
require.Equal(t, http.StatusConflict, response.StatusCode)
|
2022-02-04 17:31:24 +00:00
|
|
|
|
|
|
|
// Make User free tier again.
|
2022-02-28 21:37:46 +00:00
|
|
|
err = planet.Satellites[0].DB.Console().Users().UpdatePaidTier(ctx, planet.Uplinks[0].Projects[0].Owner.ID, false, memory.TB, memory.TB, 100000, 3)
|
2022-02-04 17:31:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Ensure User is free tier.
|
|
|
|
paid, err = planet.Satellites[0].DB.Console().Users().GetUserPaidTier(ctx, planet.Uplinks[0].Projects[0].Owner.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, paid)
|
|
|
|
|
|
|
|
req, err = http.NewRequestWithContext(ctx, http.MethodDelete, fmt.Sprintf("http://"+address.String()+"/api/projects/%s", projectID), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
|
2022-05-27 22:35:13 +01:00
|
|
|
// Project should be fine to delete now.
|
|
|
|
response, err = http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusOK, response.StatusCode)
|
|
|
|
|
2022-10-11 12:39:08 +01:00
|
|
|
responseBody, err = io.ReadAll(response.Body)
|
2022-05-27 22:35:13 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "", string(responseBody))
|
|
|
|
require.NoError(t, response.Body.Close())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestProjectDelete_withUsageCurrentMonthCharged tries to delete a last month used project of a paid tier user, which
|
|
|
|
// should fail without an accommodating invoice record. This tests creates the corresponding entry and should afterwards
|
|
|
|
// the deletion of the project.
|
|
|
|
func TestProjectDelete_withUsagePreviousMonthCharged(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
StorageNodeCount: 0,
|
|
|
|
UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: func(_ *zap.Logger, _ int, config *satellite.Config) {
|
|
|
|
config.Admin.Address = "127.0.0.1:0"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
address := planet.Satellites[0].Admin.Admin.Listener.Addr()
|
|
|
|
projectID := planet.Uplinks[0].Projects[0].ID
|
|
|
|
|
|
|
|
apiKeys, err := planet.Satellites[0].DB.Console().APIKeys().GetPagedByProjectID(ctx, projectID, console.APIKeyCursor{
|
|
|
|
Page: 1,
|
|
|
|
Limit: 2,
|
|
|
|
Search: "",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, apiKeys.APIKeys, 1)
|
|
|
|
|
|
|
|
err = planet.Satellites[0].DB.Console().APIKeys().Delete(ctx, apiKeys.APIKeys[0].ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// TODO: Improve updating of DB entries
|
|
|
|
now := time.Now().UTC()
|
|
|
|
// set fixed day to avoid failures at the end of the month
|
|
|
|
accTime := time.Date(now.Year(), now.Month()-1, 15, now.Hour(), now.Minute(), now.Second(), now.Nanosecond(), time.UTC)
|
|
|
|
tally := accounting.BucketStorageTally{
|
|
|
|
BucketName: "test",
|
|
|
|
ProjectID: projectID,
|
|
|
|
IntervalStart: accTime,
|
|
|
|
ObjectCount: 1,
|
|
|
|
TotalSegmentCount: 2,
|
|
|
|
TotalBytes: 640000,
|
|
|
|
MetadataSize: 2,
|
|
|
|
}
|
|
|
|
err = planet.Satellites[0].DB.ProjectAccounting().CreateStorageTally(ctx, tally)
|
|
|
|
require.NoError(t, err)
|
|
|
|
tally = accounting.BucketStorageTally{
|
|
|
|
BucketName: "test",
|
|
|
|
ProjectID: projectID,
|
|
|
|
IntervalStart: accTime.AddDate(0, 0, 1),
|
|
|
|
ObjectCount: 1,
|
|
|
|
TotalSegmentCount: 2,
|
|
|
|
TotalBytes: 640000,
|
|
|
|
MetadataSize: 2,
|
|
|
|
}
|
|
|
|
err = planet.Satellites[0].DB.ProjectAccounting().CreateStorageTally(ctx, tally)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Make User paid tier.
|
|
|
|
err = planet.Satellites[0].DB.Console().Users().UpdatePaidTier(ctx, planet.Uplinks[0].Projects[0].Owner.ID, true, memory.PB, memory.PB, 1000000, 3)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Ensure User is paid tier.
|
|
|
|
paid, err := planet.Satellites[0].DB.Console().Users().GetUserPaidTier(ctx, planet.Uplinks[0].Projects[0].Owner.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, paid)
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, fmt.Sprintf("http://"+address.String()+"/api/projects/%s", projectID), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
2022-10-11 12:39:08 +01:00
|
|
|
responseBody, err := io.ReadAll(response.Body)
|
2022-05-27 22:35:13 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "{\"error\":\"usage for last month exist, but is not billed yet\",\"detail\":\"\"}", string(responseBody))
|
|
|
|
require.NoError(t, response.Body.Close())
|
|
|
|
require.Equal(t, http.StatusConflict, response.StatusCode)
|
|
|
|
|
|
|
|
// Create Invoice Record for last month.
|
|
|
|
firstOfMonth := time.Date(now.Year(), now.Month()-1, 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
err = planet.Satellites[0].DB.StripeCoinPayments().ProjectRecords().Create(ctx,
|
|
|
|
[]stripecoinpayments.CreateProjectRecord{{
|
|
|
|
ProjectID: projectID,
|
|
|
|
Storage: 100,
|
|
|
|
Egress: 100,
|
|
|
|
Segments: 100}}, firstOfMonth, firstOfMonth.AddDate(0, 1, 0))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
req, err = http.NewRequestWithContext(ctx, http.MethodDelete, fmt.Sprintf("http://"+address.String()+"/api/projects/%s", projectID), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
|
|
|
|
// Project should fail to delete since the project record has not been used/billed yet.
|
|
|
|
response, err = http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
2022-10-11 12:39:08 +01:00
|
|
|
responseBody, err = io.ReadAll(response.Body)
|
2022-05-27 22:35:13 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "{\"error\":\"unapplied project invoice record exist\",\"detail\":\"\"}", string(responseBody))
|
|
|
|
require.NoError(t, response.Body.Close())
|
|
|
|
require.Equal(t, http.StatusConflict, response.StatusCode)
|
|
|
|
|
|
|
|
// Retrieve its ID and consume(mark as billed) it.
|
|
|
|
dbRecord, err := planet.Satellites[0].DB.StripeCoinPayments().ProjectRecords().Get(ctx, projectID, firstOfMonth, firstOfMonth.AddDate(0, 1, 0))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, projectID, dbRecord.ProjectID)
|
|
|
|
err = planet.Satellites[0].DB.StripeCoinPayments().ProjectRecords().Consume(ctx, dbRecord.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
req, err = http.NewRequestWithContext(ctx, http.MethodDelete, fmt.Sprintf("http://"+address.String()+"/api/projects/%s", projectID), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
|
|
|
|
// Project should be fine to delete now.
|
2022-02-04 17:31:24 +00:00
|
|
|
response, err = http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusOK, response.StatusCode)
|
|
|
|
|
2022-10-11 12:39:08 +01:00
|
|
|
responseBody, err = io.ReadAll(response.Body)
|
2022-02-04 17:31:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "", string(responseBody))
|
|
|
|
require.NoError(t, response.Body.Close())
|
2020-07-06 21:15:55 +01:00
|
|
|
})
|
|
|
|
}
|