Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a mock server to use mock server urls instead of live ones #211

Merged
merged 1 commit into from
May 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
}

}
Loading