Skip to content

Commit

Permalink
Configure SMTP for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmckendry committed Nov 10, 2023
1 parent 459f0b9 commit f17177c
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 74 deletions.
16 changes: 13 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@ on:
pull_request:
branches: [ "main" ]

jobs:
env:
SMTP_HOST: ${{ secrets.SMTP_HOST }}
SMTP_PORT: ${{ secrets.SMTP_PORT }}
SMTP_USER: ${{ secrets.SMTP_USER }}
SMTP_PASS: ${{ secrets.SMTP_PASS }}
FROM_NAME: Harry Potter
FROM_ADDRESS: 4 Privet Drive
FROM_CITY: Little Whinging, Surrey
ACCOUNT_NUMBER: 1234567890-1234
BASE_URL: https://duckduckgo.com/?q=

jobs:
test:
name: Test ccinvoice
runs-on: ubuntu-latest
Expand All @@ -23,8 +33,8 @@ jobs:
- name: Install Dependencies
run: sudo apt-get update && sudo apt-get install wkhtmltopdf

- name: Set Environment Variables & Test
- name: Go Test
# This is specifically for generating PDFs - setting this variable means we don't have to run the server to provide the HTML
# response for wkhtmltopdf to create the PDF from. We're only testing the file is created and placed in the right directory,
# so the content doesn't really matter.
run: export BASE_URL=https://duckduckgo.com/?q= && go test -v -cover
run: go test -v -cover
35 changes: 35 additions & 0 deletions data_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"os"
"testing"
)

Expand Down Expand Up @@ -237,6 +238,40 @@ func TestDeleteDog(t *testing.T) {
})
}

func TestLoadEnv(t *testing.T) {
// Create .env file if it doesn't exist
_, err := os.Stat(".env")
if os.IsNotExist(err) {
_, err := os.Create(".env")
if err != nil {
t.Errorf("error creating .env file: %v", err)
}
}

err = loadEnv()
if err != nil {
t.Errorf("loadEnv() error = %q", err)
}

// Rename .env file to force error
err = os.Rename(".env", ".env.bak")
if err != nil {
t.Errorf("error renaming .env file: %v", err)
}

err = loadEnv()
if err == nil {
t.Errorf("no error returned when expected")
}

t.Cleanup(func() {
err = os.Rename(".env.bak", ".env")
if err != nil {
t.Errorf("error renaming .env file: %v", err)
}
})
}

func createBadTable() error {
_, err := db.Exec(`
CREATE TABLE dogs (
Expand Down
87 changes: 35 additions & 52 deletions handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (
var app *fiber.App

func TestViews(t *testing.T) {
app = fiber.New(fiber.Config{
Views: html.New("./views", ".html"),
})
app.Use(recover.New())
setRoutes(app)
app = startServer()

if app == nil {
t.Error("Expected app to be initialized")
}

routes := []string{"/", "/dogs", "/dogs/add", "/dogs/edit/1", "/invoice/1", "/invoice/1/pdf"}

Expand Down Expand Up @@ -72,11 +72,7 @@ func TestViews(t *testing.T) {
}

func TestBadPaths(t *testing.T) {
app = fiber.New(fiber.Config{
Views: html.New("./views", ".html"),
})
app.Use(recover.New())
setRoutes(app)
app = startServer()

routes := []string{
"/badpath",
Expand All @@ -103,11 +99,7 @@ func TestBadPaths(t *testing.T) {
}

func TestPostReq(t *testing.T) {
app = fiber.New(fiber.Config{
Views: html.New("./views", ".html"),
})
app.Use(recover.New())
setRoutes(app)
app = startServer()

formData := url.Values{
"name": {"Rex"},
Expand Down Expand Up @@ -148,11 +140,7 @@ func TestPostReq(t *testing.T) {
}

func TestPutReq(t *testing.T) {
app = fiber.New(fiber.Config{
Views: html.New("./views", ".html"),
})
app.Use(recover.New())
setRoutes(app)
app = startServer()

formData := url.Values{
"name": {"Ralph"},
Expand Down Expand Up @@ -236,35 +224,30 @@ func TestDeleteReq(t *testing.T) {
}
}

// Ignoreing this test for now - need proper SMTP settings in place to test which I haven't configure in GH actions yet.
// func TestInvoicePostReq(t *testing.T) {
// app = fiber.New(fiber.Config{
// Views: html.New("./views", ".html"),
// })
// app.Use(recover.New())
// setRoutes(app)
//
// req := httptest.NewRequest("POST", "/invoice/2", nil)
// resp, err := app.Test(req, 10000)
// if err != nil {
// t.Error("Error sending request to Fiber: ", err)
// }
//
// if resp.StatusCode != 200 {
// t.Error("Expected status code 200, got ", resp.StatusCode)
// bodyString, _ := io.ReadAll(resp.Body)
// t.Error("Body: ", string(bodyString))
// }
//
// req = httptest.NewRequest("POST", "/invoice/abc", nil)
// resp, err = app.Test(req, 10000)
// if err != nil {
// t.Error("Error sending request to Fiber: ", err)
// }
//
// if resp.StatusCode != 400 {
// t.Error("Expected status code 400, got ", resp.StatusCode)
// bodyString, _ := io.ReadAll(resp.Body)
// t.Error("Body: ", string(bodyString))
// }
// }
func TestInvoicePostReq(t *testing.T) {
app = startServer()

req := httptest.NewRequest("POST", "/invoice/2", nil)
resp, err := app.Test(req, 10000)
if err != nil {
t.Error("Error sending request to Fiber: ", err)
}

if resp.StatusCode != 200 {
t.Error("Expected status code 200, got ", resp.StatusCode)
bodyString, _ := io.ReadAll(resp.Body)
t.Error("Body: ", string(bodyString))
}

req = httptest.NewRequest("POST", "/invoice/abc", nil)
resp, err = app.Test(req, 10000)
if err != nil {
t.Error("Error sending request to Fiber: ", err)
}

if resp.StatusCode != 400 {
t.Error("Expected status code 400, got ", resp.StatusCode)
bodyString, _ := io.ReadAll(resp.Body)
t.Error("Body: ", string(bodyString))
}
}
29 changes: 15 additions & 14 deletions invoice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,27 @@ func TestGetNextMonday(t *testing.T) {
}
}

// Test currently fails because it relies on the web server running
func TestGeneratePdf(t *testing.T) {
dog := Dog{
ID: 1,
Name: "Fido",
OwnerName: "John Doe",
Address: "123 Fake Street",
City: "Fakeville",
Email: "[email protected]",
Service: "walk",
Quantity: 1,
Price: 25,
}

want := "./public/FID" + time.Now().Format("20060102") + ".pdf"
got, err := generatePdf(dog)
got, err := generatePdf(testDog)
if err != nil {
t.Errorf("generatePdf() error = %q", err)
}
if got != want {
t.Errorf("generateInvoice() = %q, want %q", got, want)
}
}

func TestSendEmail(t *testing.T) {
err := sendEmail(testDog)
if err != nil {
t.Errorf("sendEmail() error = %q", err)
}
}

func TestSendInvoice(t *testing.T) {
err := sendInvoice(testDog.ID)
if err != nil {
t.Errorf("sendInvoice() error = %q", err)
}
}
24 changes: 19 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func main() {
err := godotenv.Load()
err := loadEnv()
if err != nil {
log.Fatal("Error loading .env file")
}
Expand All @@ -21,6 +21,23 @@ func main() {
log.Fatal("Error initializing database: ", err)
}

app := startServer()
err = app.Listen(":3000")
if err != nil {
log.Fatal("Error starting server: ", err)
}
}

func loadEnv() error {
err := godotenv.Load()
if err != nil {
return err
}

return nil
}

func startServer() *fiber.App {
app := fiber.New(fiber.Config{
Views: html.New("./views", ".html"),
})
Expand All @@ -29,8 +46,5 @@ func main() {

setRoutes(app)

err = app.Listen(":3000")
if err != nil {
log.Fatal("Error starting server: ", err)
}
return app
}

0 comments on commit f17177c

Please sign in to comment.