From 5e0d9994ed4f6d9ad37269df1c05961861e16953 Mon Sep 17 00:00:00 2001 From: ruthv1k Date: Wed, 26 Apr 2023 10:05:11 +0530 Subject: [PATCH] Add addUsers helper func to add users to collection and use it inside TestHandler --- verify/main_test.go | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/verify/main_test.go b/verify/main_test.go index ea9ba17..cae6106 100644 --- a/verify/main_test.go +++ b/verify/main_test.go @@ -25,6 +25,23 @@ func newFirestoreMockClient(ctx context.Context) *firestore.Client { return client } +func addUsers(ctx context.Context, client *firestore.Client, users []map[string]interface{}) error { + for _, user := range users { + id, ok := user["userId"].(string) + if !ok { + return fmt.Errorf("userId is missing or not a string: %v", user) + } + + delete(user, "userId") + if _, err := client.Collection("users").Doc(id).Set(ctx, user); err != nil { + return fmt.Errorf("cannot add user %s: %v", id, err) + } + + } + + return nil +} + func TestHandler(t *testing.T) { os.Setenv("environment", "test") defer os.Unsetenv("environment") @@ -42,17 +59,15 @@ func TestHandler(t *testing.T) { defer server.Close() verifiedUserId := "123" - client.Collection("users").Doc(verifiedUserId).Set(ctx, map[string]interface{}{ - "chaincode": "abcdefgh", - "profileURL": server.URL + "/profile-one", - "profileStatus": "VERIFIED", - }) unverifiedUserId := "321" - client.Collection("users").Doc(unverifiedUserId).Set(ctx, map[string]interface{}{ - "chaincode": "testchaincode", - "profileURL": server.URL + "/profile-two", - "profileStatus": "BLOCKED", - }) + users := []map[string]interface{}{ + {"userId": verifiedUserId, "chaincode": "abcdefgh", "profileURL": server.URL + "/profile-one", "profileStatus": "VERIFIED"}, + {"userId": unverifiedUserId, "chaincode": "testchaincode", "profileURL": server.URL + "/profile-two", "profileStatus": "BLOCKED"}, + } + + if err := addUsers(ctx, client, users); err != nil { + t.Fatalf("failed to add users: %v", err) + } testCases := []struct { name string