-
-
Notifications
You must be signed in to change notification settings - Fork 293
feat: add integration test infrastructure with E2E SMS tests #879
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| name: api | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| pull_request: | ||
| branches: | ||
| - main | ||
|
|
||
| permissions: | ||
| contents: read | ||
| id-token: write | ||
|
|
||
| jobs: | ||
| Test: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout 🛎 | ||
| uses: actions/checkout@v6 | ||
|
|
||
| - name: Set up Go | ||
| uses: actions/setup-go@v6 | ||
| with: | ||
| go-version: stable | ||
|
|
||
| - name: Generate Firebase credentials | ||
| run: | | ||
| bash tests/generate-firebase-credentials.sh tests/firebase-credentials.json | ||
| echo "FIREBASE_CREDENTIALS=$(jq -c . tests/firebase-credentials.json)" >> $GITHUB_ENV | ||
|
|
||
| - name: Start Services | ||
| working-directory: ./tests | ||
| run: docker compose up -d --build | ||
|
|
||
| - name: Wait for services to be healthy | ||
| working-directory: ./tests | ||
| run: | | ||
| echo "Waiting for API to be healthy..." | ||
| for i in $(seq 1 40); do | ||
| if docker compose exec api curl -sf http://localhost:8000/health >/dev/null 2>&1; then | ||
| echo "API is healthy!" | ||
| break | ||
| fi | ||
| if [ $i -eq 40 ]; then | ||
| echo "API failed to become healthy" | ||
| docker compose logs api | ||
| exit 1 | ||
| fi | ||
| echo "Attempt $i/40 - waiting 5s..." | ||
| sleep 5 | ||
| done | ||
|
|
||
| - name: Seed Database | ||
| working-directory: ./tests | ||
| run: | | ||
| echo "Waiting for seed container to finish..." | ||
| docker compose wait seed || true | ||
| sleep 2 | ||
|
|
||
| - name: Run Integration Tests | ||
| working-directory: ./tests | ||
| run: go test -v -timeout 300s ./... | ||
|
|
||
| - name: Collect Logs on Failure | ||
| if: failure() | ||
| working-directory: ./tests | ||
| run: | | ||
| docker compose logs --tail 200 | ||
|
|
||
| - name: Stop Services | ||
| if: always() | ||
| working-directory: ./tests | ||
| run: docker compose down -v | ||
|
|
||
| Deploy: | ||
| runs-on: ubuntu-latest | ||
| needs: Test | ||
| if: github.event_name == 'push' && github.ref == 'refs/heads/main' | ||
| steps: | ||
| - name: Authenticate to Google Cloud | ||
| uses: google-github-actions/auth@v2 | ||
|
Check warning on line 82 in .github/workflows/api.yml
|
||
| with: | ||
| workload_identity_provider: ${{ secrets.GCP_WORKLOAD_IDENTITY_PROVIDER }} | ||
| service_account: ${{ secrets.GCP_SERVICE_ACCOUNT }} | ||
|
|
||
| - name: Set up Cloud SDK | ||
| uses: google-github-actions/setup-gcloud@v3 | ||
|
Check warning on line 88 in .github/workflows/api.yml
|
||
|
|
||
| - name: Trigger Cloud Build Deploy 🚀 | ||
| run: | | ||
| BUILD_ID=$(gcloud builds triggers run api-httpsms-com \ | ||
| --region=global \ | ||
| --project=httpsms-86c51 \ | ||
| --sha=${{ github.sha }} \ | ||
| --format="value(metadata.build.id)") | ||
| echo "Build ID: $BUILD_ID" | ||
| echo "Streaming build logs..." | ||
| gcloud builds log "$BUILD_ID" --region=global --project=httpsms-86c51 --stream | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,3 +6,6 @@ | |
| android/app/debug/ | ||
| *main.exe* | ||
| android/app/release/ | ||
|
|
||
| tests/firebase-credentials.json | ||
| tests/emulator/emulator.exe | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package services | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
|
|
||
| "firebase.google.com/go/messaging" | ||
| "github.com/NdoleStudio/httpsms/pkg/telemetry" | ||
| "github.com/palantir/stacktrace" | ||
| ) | ||
|
|
||
| // EmulatorFCMClient sends FCM messages to the phone emulator via HTTP. | ||
| type EmulatorFCMClient struct { | ||
| httpClient *http.Client | ||
| endpoint string | ||
| logger telemetry.Logger | ||
| } | ||
|
|
||
| // NewEmulatorFCMClient creates a new EmulatorFCMClient. | ||
| func NewEmulatorFCMClient(httpClient *http.Client, endpoint string, logger telemetry.Logger) *EmulatorFCMClient { | ||
| return &EmulatorFCMClient{ | ||
| httpClient: httpClient, | ||
| endpoint: endpoint, | ||
| logger: logger, | ||
| } | ||
| } | ||
|
|
||
| // emulatorFCMRequest is the payload sent to the emulator's FCM endpoint. | ||
| type emulatorFCMRequest struct { | ||
| Message *emulatorFCMMessage `json:"message"` | ||
| } | ||
|
|
||
| type emulatorFCMMessage struct { | ||
| Token string `json:"token"` | ||
| Data map[string]string `json:"data,omitempty"` | ||
| Android *emulatorAndroid `json:"android,omitempty"` | ||
| } | ||
|
|
||
| type emulatorAndroid struct { | ||
| Priority string `json:"priority,omitempty"` | ||
| } | ||
|
|
||
| // emulatorFCMResponse is the response from the emulator. | ||
| type emulatorFCMResponse struct { | ||
| Name string `json:"name"` | ||
| } | ||
|
|
||
| // Send sends a message to the emulator's FCM endpoint. | ||
| func (c *EmulatorFCMClient) Send(ctx context.Context, message *messaging.Message) (string, error) { | ||
| payload := &emulatorFCMRequest{ | ||
| Message: &emulatorFCMMessage{ | ||
| Token: message.Token, | ||
| Data: message.Data, | ||
| }, | ||
| } | ||
| if message.Android != nil { | ||
| payload.Message.Android = &emulatorAndroid{ | ||
| Priority: message.Android.Priority, | ||
| } | ||
| } | ||
|
|
||
| body, err := json.Marshal(payload) | ||
| if err != nil { | ||
| return "", stacktrace.Propagate(err, "cannot marshal FCM request for emulator") | ||
| } | ||
|
|
||
| url := fmt.Sprintf("%s/v1/projects/httpsms-test/messages:send", c.endpoint) | ||
| req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body)) | ||
| if err != nil { | ||
| return "", stacktrace.Propagate(err, "cannot create HTTP request for emulator FCM") | ||
| } | ||
| req.Header.Set("Content-Type", "application/json") | ||
|
|
||
| resp, err := c.httpClient.Do(req) | ||
| if err != nil { | ||
| return "", stacktrace.Propagate(err, fmt.Sprintf("cannot send FCM to emulator at [%s]", url)) | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| respBody, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return "", stacktrace.Propagate(err, "cannot read emulator FCM response body") | ||
| } | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return "", stacktrace.NewError("emulator FCM returned status %d: %s", resp.StatusCode, string(respBody)) | ||
| } | ||
|
|
||
| var result emulatorFCMResponse | ||
| if err = json.Unmarshal(respBody, &result); err != nil { | ||
| return "", stacktrace.Propagate(err, "cannot decode emulator FCM response") | ||
| } | ||
|
|
||
| c.logger.Info(fmt.Sprintf("emulator FCM sent successfully: %s", result.Name)) | ||
| return result.Name, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package services | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "firebase.google.com/go/messaging" | ||
| ) | ||
|
|
||
| // FCMClient is the interface for sending Firebase Cloud Messaging notifications. | ||
| type FCMClient interface { | ||
| // Send sends a message via FCM and returns the message name on success. | ||
| Send(ctx context.Context, message *messaging.Message) (string, error) | ||
| } | ||
|
|
||
| // FirebaseFCMClient wraps the real Firebase messaging.Client. | ||
| type FirebaseFCMClient struct { | ||
| client *messaging.Client | ||
| } | ||
|
|
||
| // NewFirebaseFCMClient creates a new FirebaseFCMClient. | ||
| func NewFirebaseFCMClient(client *messaging.Client) *FirebaseFCMClient { | ||
| return &FirebaseFCMClient{client: client} | ||
| } | ||
|
|
||
| // Send sends a message via the real Firebase SDK. | ||
| func (c *FirebaseFCMClient) Send(ctx context.Context, message *messaging.Message) (string, error) { | ||
| return c.client.Send(ctx, message) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.