2020-02-07 17:24:58 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package admin_test
|
|
|
|
|
|
|
|
import (
|
2020-04-06 19:29:32 +01:00
|
|
|
"fmt"
|
2020-02-07 17:24:58 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/storj/private/testplanet"
|
|
|
|
"storj.io/storj/satellite"
|
|
|
|
)
|
|
|
|
|
2020-04-06 19:29:32 +01:00
|
|
|
func TestAPI(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{
|
|
|
|
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
|
|
|
|
config.Admin.Address = "127.0.0.1:0"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
2020-04-16 13:12:46 +01:00
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
address := satellite.Admin.Admin.Listener.Addr()
|
|
|
|
project := planet.Uplinks[0].Projects[0]
|
2020-02-07 17:24:58 +00:00
|
|
|
|
2020-04-16 13:12:46 +01:00
|
|
|
link := "http://" + address.String() + "/api/project/" + project.ID.String() + "/limit"
|
2020-02-07 17:24:58 +00:00
|
|
|
|
2020-04-06 19:29:32 +01:00
|
|
|
t.Run("GetProject", func(t *testing.T) {
|
2020-02-07 17:24:58 +00:00
|
|
|
assertGet(t, link, `{"usage":{"amount":"0 B","bytes":0},"rate":{"rps":0}}`)
|
|
|
|
})
|
|
|
|
|
2020-04-06 19:29:32 +01:00
|
|
|
t.Run("GetUser", func(t *testing.T) {
|
2020-04-16 13:12:46 +01:00
|
|
|
userLink := "http://" + address.String() + "/api/user/" + project.Owner.Email
|
2020-04-06 19:29:32 +01:00
|
|
|
expected := `{` +
|
2020-04-16 13:12:46 +01:00
|
|
|
fmt.Sprintf(`"user":{"id":"%s","fullName":"User uplink0_0","email":"%s"},`, project.Owner.ID, project.Owner.Email) +
|
|
|
|
fmt.Sprintf(`"projects":[{"id":"%s","name":"uplink0_0","description":"","ownerId":"%s"}]`, project.ID, project.Owner.ID) +
|
2020-04-06 19:29:32 +01:00
|
|
|
`}`
|
|
|
|
assertGet(t, userLink, expected)
|
|
|
|
})
|
|
|
|
|
2020-02-07 17:24:58 +00:00
|
|
|
t.Run("UpdateUsage", func(t *testing.T) {
|
|
|
|
data := url.Values{"usage": []string{"1TiB"}}
|
|
|
|
req, err := http.NewRequest(http.MethodPost, link, strings.NewReader(data.Encode()))
|
|
|
|
require.NoError(t, err)
|
|
|
|
req.Header.Set("Authorization", "very-secret-token")
|
|
|
|
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())
|
|
|
|
|
|
|
|
assertGet(t, link, `{"usage":{"amount":"1.0 TiB","bytes":1099511627776},"rate":{"rps":0}}`)
|
|
|
|
|
|
|
|
req, err = http.NewRequest(http.MethodPut, link+"?usage=1GB", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
req.Header.Set("Authorization", "very-secret-token")
|
|
|
|
|
|
|
|
response, err = http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusOK, response.StatusCode)
|
|
|
|
require.NoError(t, response.Body.Close())
|
|
|
|
|
|
|
|
assertGet(t, link, `{"usage":{"amount":"1.0 GB","bytes":1000000000},"rate":{"rps":0}}`)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("UpdateRate", func(t *testing.T) {
|
|
|
|
req, err := http.NewRequest(http.MethodPut, link+"?rate=100", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
req.Header.Set("Authorization", "very-secret-token")
|
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusOK, response.StatusCode)
|
|
|
|
require.NoError(t, response.Body.Close())
|
|
|
|
|
|
|
|
assertGet(t, link, `{"usage":{"amount":"1.0 GB","bytes":1000000000},"rate":{"rps":100}}`)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertGet(t *testing.T, link string, expected string) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, link, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
req.Header.Set("Authorization", "very-secret-token")
|
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(response.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, response.Body.Close())
|
|
|
|
|
2020-04-06 19:29:32 +01:00
|
|
|
require.Equal(t, http.StatusOK, response.StatusCode, string(data))
|
2020-02-07 17:24:58 +00:00
|
|
|
require.Equal(t, expected, string(data))
|
|
|
|
}
|