system_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package testserver
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "testing"
  7. "github.com/pkg/errors"
  8. "github.com/stretchr/testify/require"
  9. apiv1 "github.com/usememos/memos/api/v1"
  10. )
  11. func TestSystemServer(t *testing.T) {
  12. ctx := context.Background()
  13. s, err := NewTestingServer(ctx, t)
  14. require.NoError(t, err)
  15. defer s.Shutdown(ctx)
  16. status, err := s.getSystemStatus()
  17. require.NoError(t, err)
  18. require.Equal(t, (*apiv1.User)(nil), status.Host)
  19. signup := &apiv1.SignUp{
  20. Username: "testuser",
  21. Password: "testpassword",
  22. }
  23. user, err := s.postAuthSignUp(signup)
  24. require.NoError(t, err)
  25. require.Equal(t, signup.Username, user.Username)
  26. err = s.pingSystem()
  27. require.NoError(t, err)
  28. status, err = s.getSystemStatus()
  29. require.NoError(t, err)
  30. require.Equal(t, user.ID, status.Host.ID)
  31. }
  32. func (s *TestingServer) pingSystem() error {
  33. _, err := s.get("/api/v1/ping", nil)
  34. if err != nil {
  35. return err
  36. }
  37. return nil
  38. }
  39. func (s *TestingServer) getSystemStatus() (*apiv1.SystemStatus, error) {
  40. body, err := s.get("/api/v1/status", nil)
  41. if err != nil {
  42. return nil, err
  43. }
  44. buf := &bytes.Buffer{}
  45. _, err = buf.ReadFrom(body)
  46. if err != nil {
  47. return nil, errors.Wrap(err, "fail to read response body")
  48. }
  49. systemStatus := &apiv1.SystemStatus{}
  50. if err = json.Unmarshal(buf.Bytes(), systemStatus); err != nil {
  51. return nil, errors.Wrap(err, "fail to unmarshal get system status response")
  52. }
  53. return systemStatus, nil
  54. }