2020-02-07 16:36:28 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package admin_test
|
|
|
|
|
|
|
|
import (
|
2021-10-01 12:36:41 +01:00
|
|
|
"io/ioutil"
|
2020-02-07 16:36:28 +00:00
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/storj/private/testplanet"
|
|
|
|
"storj.io/storj/satellite"
|
|
|
|
)
|
|
|
|
|
2020-02-07 17:24:58 +00:00
|
|
|
func TestBasic(t *testing.T) {
|
2020-02-07 16:36:28 +00:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
StorageNodeCount: 0,
|
|
|
|
UplinkCount: 0,
|
|
|
|
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) {
|
|
|
|
sat := planet.Satellites[0]
|
|
|
|
address := sat.Admin.Admin.Listener.Addr()
|
2021-10-01 13:26:21 +01:00
|
|
|
baseURL := "http://" + address.String()
|
|
|
|
|
|
|
|
t.Run("UI", func(t *testing.T) {
|
2022-03-23 13:29:47 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, baseURL+"/.keep", nil)
|
2021-12-01 16:17:30 +00:00
|
|
|
require.NoError(t, err)
|
2021-10-01 13:26:21 +01:00
|
|
|
|
2021-12-01 16:17:30 +00:00
|
|
|
response, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
2021-10-01 13:26:21 +01:00
|
|
|
|
2021-12-01 16:17:30 +00:00
|
|
|
require.Equal(t, http.StatusOK, response.StatusCode)
|
2021-10-01 13:26:21 +01:00
|
|
|
|
2021-12-01 16:17:30 +00:00
|
|
|
content, err := ioutil.ReadAll(response.Body)
|
|
|
|
require.NoError(t, response.Body.Close())
|
2022-03-23 13:29:47 +00:00
|
|
|
require.Empty(t, content)
|
2021-12-01 16:17:30 +00:00
|
|
|
require.NoError(t, err)
|
2021-10-01 13:26:21 +01:00
|
|
|
})
|
2020-02-07 16:36:28 +00:00
|
|
|
|
2020-02-07 17:24:58 +00:00
|
|
|
t.Run("NoAccess", func(t *testing.T) {
|
2021-10-01 13:26:21 +01:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, baseURL+"/api/projects/some-id", nil)
|
2021-05-14 16:05:42 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(req)
|
2020-02-07 17:24:58 +00:00
|
|
|
require.NoError(t, err)
|
2020-02-07 16:36:28 +00:00
|
|
|
|
2020-02-07 17:24:58 +00:00
|
|
|
require.Equal(t, http.StatusForbidden, response.StatusCode)
|
2021-10-01 12:36:41 +01:00
|
|
|
require.Equal(t, "application/json", response.Header.Get("Content-Type"))
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(response.Body)
|
2020-02-07 17:24:58 +00:00
|
|
|
require.NoError(t, response.Body.Close())
|
2021-10-01 12:36:41 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, `{"error":"Forbidden","detail":""}`, string(body))
|
2020-02-07 17:24:58 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("WrongAccess", func(t *testing.T) {
|
2021-10-01 13:26:21 +01:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, baseURL+"/api/users/alice@storj.test", nil)
|
2020-02-07 17:24:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
req.Header.Set("Authorization", "wrong-key")
|
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, http.StatusForbidden, response.StatusCode)
|
2021-10-01 12:36:41 +01:00
|
|
|
require.Equal(t, "application/json", response.Header.Get("Content-Type"))
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(response.Body)
|
2020-02-07 17:24:58 +00:00
|
|
|
require.NoError(t, response.Body.Close())
|
2021-10-01 12:36:41 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, `{"error":"Forbidden","detail":""}`, string(body))
|
2020-02-07 17:24:58 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("WithAccess", func(t *testing.T) {
|
2021-10-01 13:26:21 +01:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, baseURL+"/api", 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)
|
|
|
|
|
|
|
|
// currently no main page so 404
|
|
|
|
require.Equal(t, http.StatusNotFound, response.StatusCode)
|
2021-10-01 12:36:41 +01:00
|
|
|
require.Equal(t, "text/plain; charset=utf-8", response.Header.Get("Content-Type"))
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(response.Body)
|
2020-02-07 17:24:58 +00:00
|
|
|
require.NoError(t, response.Body.Close())
|
2021-10-01 12:36:41 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Contains(t, string(body), "not found")
|
2020-02-07 17:24:58 +00:00
|
|
|
})
|
2020-02-07 16:36:28 +00:00
|
|
|
})
|
|
|
|
}
|