Skip to content

Commit 3d21021

Browse files
committed
code cleanup
1 parent 52d6257 commit 3d21021

File tree

2 files changed

+173
-93
lines changed

2 files changed

+173
-93
lines changed

auth/user_mgt.go

Lines changed: 123 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -838,61 +838,6 @@ type getAccountInfoResponse struct {
838838
Users []*userQueryResponse `json:"users"`
839839
}
840840

841-
// QueryUserInfoResponse is the response structure for the accounts:query endpoint.
842-
type QueryUserInfoResponse struct {
843-
Users []*UserRecord
844-
Count int64
845-
}
846-
847-
type queryUsersResponse struct {
848-
Users []*userQueryResponse `json:"userInfo"`
849-
Count int64 `json:"recordsCount,string,omitempty"`
850-
}
851-
852-
// SQLExpression is a query condition used to filter results.
853-
type SQLExpression struct {
854-
Email string `json:"email,omitempty"`
855-
UserID string `json:"userId,omitempty"`
856-
PhoneNumber string `json:"phoneNumber,omitempty"`
857-
}
858-
859-
// QueryUsersRequest is the request structure for the accounts:query endpoint.
860-
type QueryUsersRequest struct {
861-
ReturnUserInfo bool `json:"returnUserInfo"`
862-
Limit int64 `json:"limit,string,omitempty"`
863-
Offset int64 `json:"offset,string,omitempty"`
864-
SortBy string `json:"sortBy,omitempty"`
865-
Order string `json:"order,omitempty"`
866-
TenantID string `json:"tenantId,omitempty"`
867-
Expression []*SQLExpression `json:"expression,omitempty"`
868-
}
869-
870-
// SortByField is a field to use for sorting user accounts.
871-
type SortByField string
872-
873-
const (
874-
// UserID sorts results by userId.
875-
UserID SortByField = "USER_ID"
876-
// Name sorts results by name.
877-
Name SortByField = "NAME"
878-
// CreatedAt sorts results by createdAt.
879-
CreatedAt SortByField = "CREATED_AT"
880-
// LastLoginAt sorts results by lastLoginAt.
881-
LastLoginAt SortByField = "LAST_LOGIN_AT"
882-
// UserEmail sorts results by userEmail.
883-
UserEmail SortByField = "USER_EMAIL"
884-
)
885-
886-
// Order is an order for sorting query results.
887-
type Order string
888-
889-
const (
890-
// Asc sorts in ascending order.
891-
Asc Order = "ASC"
892-
// Desc sorts in descending order.
893-
Desc Order = "DESC"
894-
)
895-
896841
func (c *baseClient) getUser(ctx context.Context, query *userQuery) (*UserRecord, error) {
897842
var parsed getAccountInfoResponse
898843
resp, err := c.post(ctx, "/accounts:lookup", query.build(), &parsed)
@@ -1103,6 +1048,129 @@ func (c *baseClient) GetUsers(
11031048
return &GetUsersResult{userRecords, notFound}, nil
11041049
}
11051050

1051+
// QueryUserInfoResponse is the response structure for the accounts:query endpoint.
1052+
type QueryUserInfoResponse struct {
1053+
Users []*UserRecord
1054+
Count int64
1055+
}
1056+
1057+
type queryUsersResponse struct {
1058+
Users []*userQueryResponse `json:"userInfo"`
1059+
Count int64 `json:"recordsCount,string,omitempty"`
1060+
}
1061+
1062+
// SQLExpression is a query condition used to filter results.
1063+
type SQLExpression struct {
1064+
Email string `json:"email,omitempty"`
1065+
UID string `json:"userId,omitempty"`
1066+
PhoneNumber string `json:"phoneNumber,omitempty"`
1067+
}
1068+
1069+
// QueryUsersRequest is the request structure for the accounts:query endpoint.
1070+
type QueryUsersRequest struct {
1071+
ReturnUserInfo bool `json:"returnUserInfo"`
1072+
Limit int64 `json:"limit,string,omitempty"`
1073+
Offset int64 `json:"offset,string,omitempty"`
1074+
SortBy SortByField `json:"-"`
1075+
Order Order `json:"-"`
1076+
TenantID string `json:"tenantId,omitempty"`
1077+
Expression []*SQLExpression `json:"expression,omitempty"`
1078+
}
1079+
1080+
// MarshalJSON marshals a QueryUsersRequest into JSON (for internal use only).
1081+
func (q *QueryUsersRequest) MarshalJSON() ([]byte, error) {
1082+
var sortBy string
1083+
if q.SortBy != sortByUnspecified {
1084+
sortBys := map[SortByField]string{
1085+
UID: "USER_ID",
1086+
Name: "NAME",
1087+
CreatedAt: "CREATED_AT",
1088+
LastLoginAt: "LAST_LOGIN_AT",
1089+
UserEmail: "USER_EMAIL",
1090+
}
1091+
sortBy = sortBys[q.SortBy]
1092+
}
1093+
1094+
var order string
1095+
if q.Order != orderUnspecified {
1096+
orders := map[Order]string{
1097+
Asc: "ASC",
1098+
Desc: "DESC",
1099+
}
1100+
order = orders[q.Order]
1101+
}
1102+
1103+
type queryUsersRequestInternal QueryUsersRequest
1104+
temp := &struct {
1105+
SortBy string `json:"sortBy,omitempty"`
1106+
Order string `json:"order,omitempty"`
1107+
*queryUsersRequestInternal
1108+
}{
1109+
SortBy: sortBy,
1110+
Order: order,
1111+
queryUsersRequestInternal: (*queryUsersRequestInternal)(q),
1112+
}
1113+
return json.Marshal(temp)
1114+
}
1115+
1116+
// SortByField is a field to use for sorting user accounts.
1117+
type SortByField int
1118+
1119+
const (
1120+
sortByUnspecified SortByField = iota
1121+
// UID sorts results by userId.
1122+
UID
1123+
// Name sorts results by name.
1124+
Name
1125+
// CreatedAt sorts results by createdAt.
1126+
CreatedAt
1127+
// LastLoginAt sorts results by lastLoginAt.
1128+
LastLoginAt
1129+
// UserEmail sorts results by userEmail.
1130+
UserEmail
1131+
)
1132+
1133+
// Order is an order for sorting query results.
1134+
type Order int
1135+
1136+
const (
1137+
orderUnspecified Order = iota
1138+
// Asc sorts in ascending order.
1139+
Asc
1140+
// Desc sorts in descending order.
1141+
Desc
1142+
)
1143+
1144+
// QueryUsers queries for user accounts based on the provided query configuration.
1145+
func (c *baseClient) QueryUsers(ctx context.Context, query *QueryUsersRequest) (*QueryUserInfoResponse, error) {
1146+
if query == nil {
1147+
return nil, fmt.Errorf("query request must not be nil")
1148+
}
1149+
1150+
var parsed queryUsersResponse
1151+
_, err := c.post(ctx, "/accounts:query", query, &parsed)
1152+
if err != nil {
1153+
return nil, err
1154+
}
1155+
1156+
//log.Printf("QueryUsers() with response = %d, %d", parsed.Count, len(parsed.Users))
1157+
1158+
var userRecords []*UserRecord
1159+
for _, user := range parsed.Users {
1160+
userRecord, err := user.makeUserRecord()
1161+
if err != nil {
1162+
return nil, fmt.Errorf("error while parsing response: %w", err)
1163+
}
1164+
userRecords = append(userRecords, userRecord)
1165+
}
1166+
1167+
return &QueryUserInfoResponse{
1168+
Users: userRecords,
1169+
Count: parsed.Count,
1170+
}, nil
1171+
}
1172+
1173+
11061174
type userQueryResponse struct {
11071175
UID string `json:"localId,omitempty"`
11081176
DisplayName string `json:"displayName,omitempty"`
@@ -1366,35 +1434,6 @@ type DeleteUsersErrorInfo struct {
13661434
// array of errors that correspond to the failed deletions. An error is
13671435
// returned if any of the identifiers are invalid or if more than 1000
13681436
// identifiers are specified.
1369-
// QueryUsers queries for user accounts based on the provided query configuration.
1370-
func (c *baseClient) QueryUsers(ctx context.Context, query *QueryUsersRequest) (*QueryUserInfoResponse, error) {
1371-
if query == nil {
1372-
return nil, fmt.Errorf("query request must not be nil")
1373-
}
1374-
1375-
var parsed queryUsersResponse
1376-
_, err := c.post(ctx, "/accounts:query", query, &parsed)
1377-
if err != nil {
1378-
return nil, err
1379-
}
1380-
1381-
//log.Printf("QueryUsers() with response = %d, %d", parsed.Count, len(parsed.Users))
1382-
1383-
var userRecords []*UserRecord
1384-
for _, user := range parsed.Users {
1385-
userRecord, err := user.makeUserRecord()
1386-
if err != nil {
1387-
return nil, fmt.Errorf("error while parsing response: %w", err)
1388-
}
1389-
userRecords = append(userRecords, userRecord)
1390-
}
1391-
1392-
return &QueryUserInfoResponse{
1393-
Users: userRecords,
1394-
Count: parsed.Count,
1395-
}, nil
1396-
}
1397-
13981437
func (c *baseClient) DeleteUsers(ctx context.Context, uids []string) (*DeleteUsersResult, error) {
13991438
if len(uids) == 0 {
14001439
return &DeleteUsersResult{}, nil

auth/user_mgt_test.go

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1901,7 +1901,7 @@ func TestDeleteUsers(t *testing.T) {
19011901

19021902
func TestQueryUsers(t *testing.T) {
19031903
resp := `{
1904-
"usersInfo": [{
1904+
"userInfo": [{
19051905
"localId": "testuser",
19061906
"email": "[email protected]",
19071907
"phoneNumber": "+1234567890",
@@ -1944,9 +1944,9 @@ func TestQueryUsers(t *testing.T) {
19441944

19451945
query := &QueryUsersRequest{
19461946
ReturnUserInfo: true,
1947-
Limit: "1",
1948-
SortBy: string(UserEmail),
1949-
Order: string(Asc),
1947+
Limit: 1,
1948+
SortBy: UserEmail,
1949+
Order: Asc,
19501950
Expression: []*SQLExpression{
19511951
{
19521952
@@ -1963,8 +1963,8 @@ func TestQueryUsers(t *testing.T) {
19631963
t.Fatalf("QueryUsers() returned %d users; want 1", len(result.Users))
19641964
}
19651965

1966-
if result.Count != "1" {
1967-
t.Errorf("QueryUsers() returned count %q; want '1'", result.Count)
1966+
if result.Count != 1 {
1967+
t.Errorf("QueryUsers() returned count %d; want 1", result.Count)
19681968
}
19691969

19701970
if !reflect.DeepEqual(result.Users[0], testUser) {
@@ -1989,9 +1989,9 @@ func TestQueryUsersError(t *testing.T) {
19891989

19901990
query := &QueryUsersRequest{
19911991
ReturnUserInfo: true,
1992-
Limit: "1",
1993-
SortBy: "USER_EMAIL",
1994-
Order: "ASC",
1992+
Limit: 1,
1993+
SortBy: UserEmail,
1994+
Order: Asc,
19951995
Expression: []*SQLExpression{
19961996
{
19971997
@@ -2005,6 +2005,47 @@ func TestQueryUsersError(t *testing.T) {
20052005
}
20062006
}
20072007

2008+
func TestQueryUsersNilQuery(t *testing.T) {
2009+
s := echoServer([]byte("{}"), t)
2010+
defer s.Close()
2011+
result, err := s.Client.QueryUsers(context.Background(), nil)
2012+
if result != nil || err == nil {
2013+
t.Fatalf("QueryUsers(nil) = (%v, %v); want = (nil, error)", result, err)
2014+
}
2015+
}
2016+
2017+
func TestQueryUsersMalformedCustomAttributes(t *testing.T) {
2018+
resp := `{
2019+
"userInfo": [{
2020+
"localId": "testuser",
2021+
"customAttributes": "invalid-json"
2022+
}]
2023+
}`
2024+
s := echoServer([]byte(resp), t)
2025+
defer s.Close()
2026+
query := &QueryUsersRequest{}
2027+
result, err := s.Client.QueryUsers(context.Background(), query)
2028+
if result != nil || err == nil {
2029+
t.Fatalf("QueryUsers() = (%v, %v); want = (nil, error)", result, err)
2030+
}
2031+
}
2032+
2033+
func TestQueryUsersMalformedLastRefreshTimestamp(t *testing.T) {
2034+
resp := `{
2035+
"userInfo": [{
2036+
"localId": "testuser",
2037+
"lastRefreshAt": "invalid-timestamp"
2038+
}]
2039+
}`
2040+
s := echoServer([]byte(resp), t)
2041+
defer s.Close()
2042+
query := &QueryUsersRequest{}
2043+
result, err := s.Client.QueryUsers(context.Background(), query)
2044+
if result != nil || err == nil {
2045+
t.Fatalf("QueryUsers() = (%v, %v); want = (nil, error)", result, err)
2046+
}
2047+
}
2048+
20082049
func TestMakeExportedUser(t *testing.T) {
20092050
queryResponse := &userQueryResponse{
20102051
UID: "testuser",

0 commit comments

Comments
 (0)