Skip to content

Commit

Permalink
feat/SSM-36: added some tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
j1mb0b committed Feb 4, 2025
1 parent e8d22b1 commit 129bb05
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions service-app/internal/api/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@ package api

import (
"bytes"
"context"
"encoding/xml"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/ministryofjustice/opg-scanning/config"
"github.com/ministryofjustice/opg-scanning/internal/auth"
"github.com/ministryofjustice/opg-scanning/internal/aws"
"github.com/ministryofjustice/opg-scanning/internal/httpclient"
"github.com/ministryofjustice/opg-scanning/internal/ingestion"
"github.com/ministryofjustice/opg-scanning/internal/logger"
"github.com/ministryofjustice/opg-scanning/internal/types"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

var xmlPayload = `
Expand Down Expand Up @@ -96,3 +103,40 @@ func TestIngestHandler_InvalidXML(t *testing.T) {
t.Errorf("expected status %d; got %d", http.StatusBadRequest, resp.StatusCode)
}
}

func TestProcessAndPersist_IncludesXMLDeclaration(t *testing.T) {
controller := setupController()

var setPayload types.BaseSet
err := xml.Unmarshal([]byte(xmlPayload), &setPayload)
require.NoError(t, err, "failed to unmarshal xmlPayload")

originalDoc := &types.BaseDocument{
Type: "LP1F",
}

var capturedXML []byte

mockAws := new(aws.MockAwsClient)
controller.AwsClient = mockAws

// Set expectation on PersistFormData.
mockAws.
On("PersistFormData", mock.Anything, mock.AnythingOfType("*bytes.Reader"), "LP1F").
Return("testFileName", nil).
Run(func(args mock.Arguments) {
bodyReader := args.Get(1).(io.Reader)
var err error
capturedXML, err = io.ReadAll(bodyReader)
require.NoError(t, err)
})
fileName, err := controller.processAndPersist(context.Background(), setPayload, originalDoc)
require.NoError(t, err)
require.Equal(t, "testFileName", fileName)

// Verify that the captured XML starts with the XML declaration.
expectedHeader := `<?xml version="1.0" encoding="UTF-8" standalone="no"?>`
if !strings.HasPrefix(string(capturedXML), expectedHeader) {
t.Errorf("expected XML to begin with header %q, got: %s", expectedHeader, string(capturedXML))
}
}

0 comments on commit 129bb05

Please sign in to comment.