From 48b580910464898ab538d7e7415bdc2004ce4c52 Mon Sep 17 00:00:00 2001 From: Fube <8064235+Fube@users.noreply.github.com> Date: Thu, 25 Jul 2024 20:32:29 -0400 Subject: [PATCH] Get random port for mailer tests (#605) JIRA: link to jira ticket ## Context: What is the ticket about and why are we doing this change. ## Changes What are the various changes and what other modules do those changes effect. This can be bullet point or sentence format. ## Before and After UI (Required for UI-impacting PRs) If this is a change to the UI, include before and after screenshots to show the differences. If this is a new UI feature, include screenshots to show reviewers what it looks like. ## Dev notes (Optional) - Specific technical changes that should be noted ## Linked pull requests (Optional) - pull request link --- .github/workflows/go.yml | 2 +- mailer-service/Dockerfile | 2 +- .../controller/mailer.controller_test.go | 45 ++++++++++++------- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index fa41792a0a..7ebb1f0a79 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -15,7 +15,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v2 with: - go-version: 1.17 + go-version: '1.20' - name: Install Go Swagger CLI run: go install github.com/swaggo/swag/cmd/swag@v1.3.2 diff --git a/mailer-service/Dockerfile b/mailer-service/Dockerfile index c0af5404b7..bb4383effc 100644 --- a/mailer-service/Dockerfile +++ b/mailer-service/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.17.1 AS builder +FROM golang:1.20 AS builder WORKDIR /app/GoMailer COPY . . RUN ["go", "install", "github.com/swaggo/swag/cmd/swag@v1.3.2"] diff --git a/mailer-service/mailer/controller/mailer.controller_test.go b/mailer-service/mailer/controller/mailer.controller_test.go index cff64ed503..7b1d5a5ab5 100644 --- a/mailer-service/mailer/controller/mailer.controller_test.go +++ b/mailer-service/mailer/controller/mailer.controller_test.go @@ -87,7 +87,7 @@ func TestMailerControllerImpl_Unmarshalls(t *testing.T) { assert.Equal(t, http.StatusOK, w.Code) body, err := ioutil.ReadAll(w.Result().Body) assert.Nil(t, err) - assert.Contains(t, string(body), "Message sent to " + email) + assert.Contains(t, string(body), "Message sent to "+email) return true }) } @@ -118,14 +118,13 @@ func TestMailerControllerImpl_ValidateInValidEmail(t *testing.T) { assert.Nil(t, err) assert.Equal(t, - "\"Key: 'Mail.To' Error:Field validation for 'To' failed on the 'required' tag" + + "\"Key: 'Mail.To' Error:Field validation for 'To' failed on the 'required' tag"+ "\\nKey: 'Mail.Message' Error:Field validation for 'Message' failed on the 'required' tag\"", string(body)) return true }) - } func TestHandleMailPOST_ValidMail(t *testing.T) { @@ -163,30 +162,42 @@ func TestHandleMailPOST_NilMail(t *testing.T) { assert.Equal(t, "\"Unable to parse e-mail from body\"", recorder.Body.String()) } +func getRandomPort() int { + s, e := net.Listen("tcp", ":0") + if s == nil || e != nil { + panic("Unable to create a new port") + } + defer s.Close() + + return s.Addr().(*net.TCPAddr).Port +} + func TestHandleMailPOST_Full(t *testing.T) { - get, err := startMockSMTPServer(2000) + port := getRandomPort() + get, err := startMockSMTPServer(port) + fmt.Println(err) assert.Nil(t, err) - fullTestEnv(t, func (engine *gin.Engine, req *http.Request, m *mailer.Mail) { + fullTestEnv(t, port, func(engine *gin.Engine, req *http.Request, m *mailer.Mail) { testHTTPResponse(t, engine, req, func(w *httptest.ResponseRecorder) bool { - assert.Equal(t, http.StatusOK, w.Code) - got, err := get() - assert.Nil(t, err) - assert.Contains(t, got, "To: " + m.To) - assert.Contains(t, got, "Subject: " + m.Subject) - assert.Contains(t, got, m.Subject) - assert.Equal(t, fmt.Sprintf("\"Message sent to %s\"", m.To), w.Body.String()) - return true + assert.Equal(t, http.StatusOK, w.Code) + got, err := get() + assert.Nil(t, err) + assert.Contains(t, got, "To: "+m.To) + assert.Contains(t, got, "Subject: "+m.Subject) + assert.Contains(t, got, m.Subject) + assert.Equal(t, fmt.Sprintf("\"Message sent to %s\"", m.To), w.Body.String()) + return true }) }) } func TestHandleMailPOST_FullInValid(t *testing.T) { - fullTestEnv(t, func (engine *gin.Engine, req *http.Request, _ *mailer.Mail) { + fullTestEnv(t, getRandomPort(), func(engine *gin.Engine, req *http.Request, _ *mailer.Mail) { testHTTPResponse(t, engine, req, func(w *httptest.ResponseRecorder) bool { @@ -197,10 +208,10 @@ func TestHandleMailPOST_FullInValid(t *testing.T) { }) } - // Absolute legend https://titanwolf.org/Network/Articles/Article?AID=5749f0a3-9be8-4add-a1d3-9699e7554251#gsc.tab=0 type receivedMailTextGetter func() (string, error) + func startMockSMTPServer(port int, serverResponses ...string) (receivedMailTextGetter, error) { if len(serverResponses) == 0 { // default server responses @@ -289,12 +300,12 @@ func startMockSMTPServer(port int, serverResponses ...string) (receivedMailTextG return getReceivedData, nil } -func fullTestEnv(t *testing.T, f func(e *gin.Engine, r *http.Request, m *mailer.Mail)) { +func fullTestEnv(t *testing.T, port int, f func(e *gin.Engine, r *http.Request, m *mailer.Mail)) { engine := gin.Default() mS := service.MailerServiceImpl{} - dialer := mailer.CreateDialer("localhost", "a@b.c", "pass", 2000) + dialer := mailer.CreateDialer("localhost", "a@b.c", "pass", port) mS.New(dialer) mC := MailerControllerImpl{}