fix: return not found for missing internal user
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/tinode/chat/server/store"
|
||||
"github.com/tinode/chat/server/store/mock_store"
|
||||
"github.com/tinode/chat/server/store/types"
|
||||
)
|
||||
|
||||
type internalAdminQueryNotFound struct{}
|
||||
|
||||
func (internalAdminQueryNotFound) Query(types.TenantID, string, string, []any, int, int) (*types.InternalAdminQueryResult, error) {
|
||||
return nil, types.ErrNotFound
|
||||
}
|
||||
|
||||
func (internalAdminQueryNotFound) QueryOne(types.TenantID, string, []any) (map[string]any, error) {
|
||||
return nil, types.ErrNotFound
|
||||
}
|
||||
|
||||
func TestServeInternalUserQueryMissingUserReturnsNotFound(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
oldQueries := store.InternalAdminQueries
|
||||
oldUsers := store.Users
|
||||
defer func() {
|
||||
store.InternalAdminQueries = oldQueries
|
||||
store.Users = oldUsers
|
||||
}()
|
||||
|
||||
store.InternalAdminQueries = internalAdminQueryNotFound{}
|
||||
users := mock_store.NewMockUsersPersistenceInterface(ctrl)
|
||||
users.EXPECT().Get(types.TenantID(1), gomock.Any()).Return(nil, nil)
|
||||
store.Users = users
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/internal/admin-query/tenants/default/users/1566402774239809500", nil)
|
||||
wrt := httptest.NewRecorder()
|
||||
serveInternalUserQuery(wrt, req, types.TenantID(1), "1566402774239809500")
|
||||
|
||||
if wrt.Code != http.StatusNotFound {
|
||||
t.Fatalf("unexpected status: got %d, want %d; body=%s", wrt.Code, http.StatusNotFound, wrt.Body.String())
|
||||
}
|
||||
var resp imHTTPResponse
|
||||
if err := json.Unmarshal(wrt.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("failed to decode response: %v", err)
|
||||
}
|
||||
if resp.Code != 30004 {
|
||||
t.Fatalf("unexpected response code: got %d, want 30004", resp.Code)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user