2022-06-16 03:07:38 +01:00
|
|
|
// Copyright (C) 2022 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package apigen_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap/zaptest"
|
|
|
|
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/uuid"
|
|
|
|
"storj.io/storj/private/api"
|
|
|
|
"storj.io/storj/private/apigen"
|
|
|
|
"storj.io/storj/private/apigen/example"
|
2023-09-25 13:08:33 +01:00
|
|
|
"storj.io/storj/private/apigen/example/myapi"
|
2022-06-16 03:07:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2023-11-10 17:31:11 +00:00
|
|
|
auth struct{}
|
|
|
|
service struct{}
|
2022-06-16 03:07:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func (a auth) IsAuthenticated(ctx context.Context, r *http.Request, isCookieAuth, isKeyAuth bool) (context.Context, error) {
|
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a auth) RemoveAuthCookie(w http.ResponseWriter) {}
|
|
|
|
|
2023-09-25 17:43:30 +01:00
|
|
|
func (s service) Get(
|
|
|
|
ctx context.Context,
|
2023-11-10 17:31:11 +00:00
|
|
|
) ([]myapi.Document, api.HTTPError) {
|
|
|
|
return []myapi.Document{}, api.HTTPError{}
|
2023-09-25 17:43:30 +01:00
|
|
|
}
|
|
|
|
|
2023-09-25 13:08:33 +01:00
|
|
|
func (s service) GetOne(
|
|
|
|
ctx context.Context,
|
|
|
|
pathParam string,
|
|
|
|
) (*myapi.Document, api.HTTPError) {
|
|
|
|
return &myapi.Document{}, api.HTTPError{}
|
|
|
|
}
|
|
|
|
|
2023-09-25 17:43:30 +01:00
|
|
|
func (s service) GetTag(
|
|
|
|
ctx context.Context,
|
|
|
|
pathParam string,
|
|
|
|
tagName string,
|
|
|
|
) (*[2]string, api.HTTPError) {
|
|
|
|
return &[2]string{}, api.HTTPError{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s service) GetVersions(
|
|
|
|
ctx context.Context,
|
|
|
|
pathParam string,
|
|
|
|
) ([]myapi.Version, api.HTTPError) {
|
|
|
|
return []myapi.Version{}, api.HTTPError{}
|
|
|
|
}
|
|
|
|
|
2023-08-28 14:43:15 +01:00
|
|
|
func (s service) UpdateContent(
|
2023-08-24 17:28:00 +01:00
|
|
|
ctx context.Context,
|
|
|
|
pathParam string,
|
|
|
|
id uuid.UUID,
|
|
|
|
date time.Time,
|
2023-11-10 17:31:11 +00:00
|
|
|
body myapi.NewDocument,
|
|
|
|
) (*myapi.Document, api.HTTPError) {
|
|
|
|
return &myapi.Document{
|
2022-06-16 03:07:38 +01:00
|
|
|
ID: id,
|
|
|
|
Date: date,
|
|
|
|
PathParam: pathParam,
|
|
|
|
Body: body.Content,
|
|
|
|
}, api.HTTPError{}
|
|
|
|
}
|
|
|
|
|
2023-11-10 17:31:11 +00:00
|
|
|
func send(ctx context.Context, t *testing.T, method string, url string, body interface{}) ([]byte, error) {
|
|
|
|
t.Helper()
|
|
|
|
|
2022-06-16 03:07:38 +01:00
|
|
|
var bodyReader io.Reader = http.NoBody
|
|
|
|
if body != nil {
|
|
|
|
bodyJSON, err := json.Marshal(body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
bodyReader = bytes.NewBuffer(bodyJSON)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, method, url, bodyReader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-10 17:31:11 +00:00
|
|
|
if c := resp.StatusCode; c != http.StatusOK {
|
|
|
|
t.Fatalf("unexpected status code. Want=%d, Got=%d", http.StatusOK, c)
|
|
|
|
}
|
|
|
|
|
2022-10-11 12:39:08 +01:00
|
|
|
respBody, err := io.ReadAll(resp.Body)
|
2022-06-16 03:07:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := resp.Body.Close(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return respBody, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPIServer(t *testing.T) {
|
|
|
|
ctx := testcontext.NewWithTimeout(t, 5*time.Second)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
router := mux.NewRouter()
|
2023-08-28 14:43:15 +01:00
|
|
|
example.NewDocuments(zaptest.NewLogger(t), monkit.Package(), service{}, router, auth{})
|
2022-06-16 03:07:38 +01:00
|
|
|
|
|
|
|
server := httptest.NewServer(router)
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
id, err := uuid.New()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-11-10 17:31:11 +00:00
|
|
|
expected := myapi.Document{
|
2022-06-16 03:07:38 +01:00
|
|
|
ID: id,
|
|
|
|
Date: time.Now(),
|
|
|
|
PathParam: "foo",
|
|
|
|
Body: "bar",
|
|
|
|
}
|
|
|
|
|
2023-11-10 17:31:11 +00:00
|
|
|
resp, err := send(ctx, t, http.MethodPost,
|
2023-08-28 14:43:15 +01:00
|
|
|
fmt.Sprintf("%s/api/v0/docs/%s?id=%s&date=%s",
|
2022-06-16 03:07:38 +01:00
|
|
|
server.URL,
|
|
|
|
expected.PathParam,
|
|
|
|
url.QueryEscape(expected.ID.String()),
|
|
|
|
url.QueryEscape(expected.Date.Format(apigen.DateFormat)),
|
|
|
|
), struct{ Content string }{expected.Body},
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-11-10 17:31:11 +00:00
|
|
|
fmt.Println(string(resp))
|
|
|
|
|
|
|
|
var actual map[string]any
|
2022-06-16 03:07:38 +01:00
|
|
|
require.NoError(t, json.Unmarshal(resp, &actual))
|
|
|
|
|
2023-08-24 17:28:00 +01:00
|
|
|
for _, key := range []string{"id", "date", "pathParam", "body"} {
|
2022-06-16 03:07:38 +01:00
|
|
|
require.Contains(t, actual, key)
|
|
|
|
}
|
2023-11-10 17:31:11 +00:00
|
|
|
require.Equal(t, expected.ID.String(), actual["id"].(string))
|
|
|
|
require.Equal(t, expected.Date.Format(apigen.DateFormat), actual["date"].(string))
|
|
|
|
require.Equal(t, expected.PathParam, actual["pathParam"].(string))
|
|
|
|
require.Equal(t, expected.Body, actual["body"].(string))
|
2022-06-16 03:07:38 +01:00
|
|
|
}
|