Skip to content

Commit

Permalink
Add addUsers helper func to add users to collection and use it inside…
Browse files Browse the repository at this point in the history
… TestHandler
  • Loading branch information
ruthv1k authored and lakshayman committed Apr 28, 2023
1 parent 53be50f commit 5e0d999
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions verify/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down

0 comments on commit 5e0d999

Please sign in to comment.