2023-04-17 21:34:59 +08:00
|
|
|
package testserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-09-17 22:55:13 +08:00
|
|
|
|
2023-06-17 21:25:46 +08:00
|
|
|
apiv1 "github.com/usememos/memos/api/v1"
|
2023-04-17 21:34:59 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSystemServer(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
s, err := NewTestingServer(ctx, t)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer s.Shutdown(ctx)
|
|
|
|
|
|
|
|
status, err := s.getSystemStatus()
|
|
|
|
require.NoError(t, err)
|
2023-07-02 18:56:25 +08:00
|
|
|
require.Equal(t, (*apiv1.User)(nil), status.Host)
|
2023-04-17 21:34:59 +08:00
|
|
|
|
2023-06-17 21:25:46 +08:00
|
|
|
signup := &apiv1.SignUp{
|
2023-04-17 21:34:59 +08:00
|
|
|
Username: "testuser",
|
|
|
|
Password: "testpassword",
|
|
|
|
}
|
2023-08-10 09:01:38 +08:00
|
|
|
user, err := s.postAuthSignUp(signup)
|
2023-04-17 21:34:59 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, signup.Username, user.Username)
|
2023-08-10 09:01:38 +08:00
|
|
|
err = s.pingSystem()
|
|
|
|
require.NoError(t, err)
|
2023-04-17 21:34:59 +08:00
|
|
|
status, err = s.getSystemStatus()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, user.ID, status.Host.ID)
|
|
|
|
}
|
|
|
|
|
2023-08-10 09:01:38 +08:00
|
|
|
func (s *TestingServer) pingSystem() error {
|
|
|
|
_, err := s.get("/api/v1/ping", nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-02 18:56:25 +08:00
|
|
|
func (s *TestingServer) getSystemStatus() (*apiv1.SystemStatus, error) {
|
|
|
|
body, err := s.get("/api/v1/status", nil)
|
2023-04-17 21:34:59 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
_, err = buf.ReadFrom(body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "fail to read response body")
|
|
|
|
}
|
|
|
|
|
2023-07-02 18:56:25 +08:00
|
|
|
systemStatus := &apiv1.SystemStatus{}
|
|
|
|
if err = json.Unmarshal(buf.Bytes(), systemStatus); err != nil {
|
2023-04-17 21:34:59 +08:00
|
|
|
return nil, errors.Wrap(err, "fail to unmarshal get system status response")
|
|
|
|
}
|
2023-07-02 18:56:25 +08:00
|
|
|
return systemStatus, nil
|
2023-04-17 21:34:59 +08:00
|
|
|
}
|