Skip to content

Commit

Permalink
added a mock server to use mock server urls instead of live ones (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
rishirishhh authored May 27, 2024
1 parent 56c0206 commit 915645d
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions verify/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"fmt"
"log"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
Expand All @@ -23,6 +25,14 @@ func newFirestoreMockClient(ctx context.Context) *firestore.Client {
return client
}

func startMockServer(responseBody string, responseStatusCode int) *httptest.Server {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(responseStatusCode)
w.Write([]byte(responseBody))
})
return httptest.NewServer(handler)
}

func addUsers(ctx context.Context, client *firestore.Client, users []map[string]interface{}) error {
for _, user := range users {
id, ok := user["userId"].(string)
Expand All @@ -48,11 +58,18 @@ func TestHandler(t *testing.T) {
client := newFirestoreMockClient(ctx)
defer cancel()

// Mock servers for profile verification
verifiedMockServer := startMockServer(`{"hash":"correcthash"}`, http.StatusOK)
defer verifiedMockServer.Close()

unverifiedMockServer := startMockServer(`{"hash":"incorrecthash"}`, http.StatusOK)
defer unverifiedMockServer.Close()

verifiedUserId := "123"
unverifiedUserId := "321"
users := []map[string]interface{}{
{"userId": verifiedUserId, "chaincode": "TESTCHAIN", "profileURL": "https://test-profile-service-rds.onrender.com", "profileStatus": "VERIFIED"},
{"userId": unverifiedUserId, "chaincode": "TESTCHAINCODE", "profileURL": "https://test-profile-service-rds.onrender.com", "profileStatus": "BLOCKED"},
{"userId": verifiedUserId, "chaincode": "TESTCHAIN", "profileURL": verifiedMockServer.URL, "profileStatus": "VERIFIED"},
{"userId": unverifiedUserId, "chaincode": "TESTCHAINCODE", "profileURL": unverifiedMockServer.URL, "profileStatus": "BLOCKED"},
}

if err := addUsers(ctx, client, users); err != nil {
Expand Down Expand Up @@ -97,5 +114,4 @@ func TestHandler(t *testing.T) {
assert.Equal(t, testCase.expect, response.Body)
})
}

}

0 comments on commit 915645d

Please sign in to comment.