Skip to content

Commit

Permalink
Merge pull request #249 from cybertec-postgresql/248_ory_mail
Browse files Browse the repository at this point in the history
[*] switch to ory/mail from abandoned gomail, closes #248
  • Loading branch information
pashagolub committed Apr 19, 2021
2 parents 39ed2d0 + 21858fd commit 16b9421
Show file tree
Hide file tree
Showing 15 changed files with 32 additions and 25 deletions.
3 changes: 1 addition & 2 deletions go.mod
Expand Up @@ -9,10 +9,9 @@ require (
github.com/jackc/pgtype v1.7.0
github.com/jackc/pgx/v4 v4.11.0
github.com/jessevdk/go-flags v1.5.0
github.com/ory/mail/v3 v3.0.1-0.20210418065910-7f033ddea8dc
github.com/pashagolub/pgxmock v0.0.0-20210329125448-d2e8b19a1746
github.com/sirupsen/logrus v1.8.1
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.7.0
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
)
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -324,6 +324,8 @@ github.com/openzipkin-contrib/zipkin-go-opentracing v0.4.5/go.mod h1:/wsWhb9smxS
github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw=
github.com/openzipkin/zipkin-go v0.2.1/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnhQw8ySjnjRyN4=
github.com/openzipkin/zipkin-go v0.2.2/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnhQw8ySjnjRyN4=
github.com/ory/mail/v3 v3.0.1-0.20210418065910-7f033ddea8dc h1:BU12v9x5hvONtYU2R2LnlkxmWSsjzco046NzJLcWMHg=
github.com/ory/mail/v3 v3.0.1-0.20210418065910-7f033ddea8dc/go.mod h1:vAPEMm1zIQKGmM9hcZTSlOU/CDVCXHGOw6SFxPlSoHw=
github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIwwtUjcrb0b5/5kLM=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pashagolub/pgxmock v0.0.0-20210329125448-d2e8b19a1746 h1:6XFIVysfnfY9lzajCIBeo9YsZikKMKrFaQBuat6lByI=
Expand Down Expand Up @@ -616,8 +618,6 @@ gopkg.in/cheggaaa/pb.v1 v1.0.25/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qS
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/gcfg.v1 v1.2.3/go.mod h1:yesOnuUOFQAhST5vPY4nbZsb/huCgGGXlipJsBn0b3o=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw=
gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s=
gopkg.in/ini.v1 v1.51.0 h1:AQvPpx3LzTDM0AjnIRlVFwFFGC+npRopjZxLJj6gdno=
gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
Expand Down
1 change: 1 addition & 0 deletions internal/pgengine/bootstrap.go
Expand Up @@ -234,6 +234,7 @@ func (pge *PgEngine) Finalize() {
if err != nil {
pge.l.WithError(err).Error("Cannot finalize database session")
}
pge.ConfigDb.Close()
pge.ConfigDb = nil
}

Expand Down
5 changes: 4 additions & 1 deletion internal/pgengine/bootstrap_test.go
Expand Up @@ -91,8 +91,10 @@ func TestReconnectAndFixLeftovers(t *testing.T) {
func TestFinalizeConnection(t *testing.T) {
initmockdb(t)
mockpge := pgengine.NewDB(mockPool, "pgengine_unit_test")
mockPool.ExpectClose().WillReturnError(errors.New("expected"))
mockPool.ExpectExec(`DELETE FROM timetable\.active_session`).WillReturnResult(pgxmock.NewResult("EXECUTE", 0))
mockPool.ExpectClose()
mockpge.Finalize()
assert.NoError(t, mockPool.ExpectationsWereMet())
}

type mockpgrow struct {
Expand Down Expand Up @@ -127,6 +129,7 @@ func (m mockpgconn) QueryRow(context.Context, string, ...interface{}) pgx.Row {

func TestTryLockClientName(t *testing.T) {
pge := pgengine.NewDB(nil, "pgengine_unit_test")
defer func() { pge = nil }()
t.Run("query error", func(t *testing.T) {
r := &mockpgrow{}
m := mockpgconn{r}
Expand Down
2 changes: 1 addition & 1 deletion internal/pgengine/migration.go
Expand Up @@ -59,7 +59,7 @@ func (pge *PgEngine) initMigrator() error {
}
var err error
m, err = migrator.New(
migrator.TableName("timetable.migration"),
migrator.TableName("timetable.migrations"),
migrator.SetNotice(func(s string) {
pge.l.Info(s)
}),
Expand Down
2 changes: 2 additions & 0 deletions internal/pgengine/migration_test.go
Expand Up @@ -24,4 +24,6 @@ func TestMigrations(t *testing.T) {
assert.NoError(t, err)
assert.True(t, ok, "Should need migrations")
assert.True(t, pge.MigrateDb(ctx), "Migrations should be applied")
_, err = pge.ConfigDb.Exec(ctx, "DROP SCHEMA IF EXISTS timetable CASCADE")
assert.NoError(t, err)
}
1 change: 0 additions & 1 deletion internal/pgengine/pgengine_test.go
Expand Up @@ -249,7 +249,6 @@ func TestSamplesScripts(t *testing.T) {
files, err := ioutil.ReadDir("../../samples")
assert.NoError(t, err, "Cannot read samples directory")
l := log.Init("error")

for _, f := range files {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
Expand Down
1 change: 0 additions & 1 deletion internal/scheduler/scheduler_test.go
Expand Up @@ -32,7 +32,6 @@ func SetupTestCase(t *testing.T) func(t *testing.T) {
}
return func(t *testing.T) {
_, _ = pge.ConfigDb.Exec(context.Background(), "DROP SCHEMA IF EXISTS timetable CASCADE")
pge.Finalize()
t.Log("Test schema dropped")
}
}
Expand Down
4 changes: 3 additions & 1 deletion internal/scheduler/shell.go
Expand Up @@ -16,7 +16,9 @@ type commander interface {
type realCommander struct{}

func (c realCommander) CombinedOutput(ctx context.Context, command string, args ...string) ([]byte, error) {
return exec.CommandContext(ctx, command, args...).CombinedOutput()
cmd := exec.CommandContext(ctx, command, args...)
cmd.Stdin = nil
return cmd.CombinedOutput()
}

// Cmd executes a command
Expand Down
2 changes: 1 addition & 1 deletion internal/scheduler/tasks.go
Expand Up @@ -65,7 +65,7 @@ func taskSendMail(ctx context.Context, sch *Scheduler, paramValues string) (stdo
if err := json.Unmarshal([]byte(paramValues), &conn); err != nil {
return "", err
}
return "", tasks.SendMail(conn)
return "", tasks.SendMail(ctx, conn)
}

func taskCopyFromFile(ctx context.Context, sch *Scheduler, val string) (stdout string, err error) {
Expand Down
1 change: 0 additions & 1 deletion internal/tasks/database.go

This file was deleted.

14 changes: 8 additions & 6 deletions internal/tasks/mail.go
@@ -1,7 +1,9 @@
package tasks

import (
"gopkg.in/gomail.v2"
"context"

gomail "github.com/ory/mail/v3"
)

type EmailConn struct {
Expand All @@ -21,14 +23,14 @@ type EmailConn struct {

// Dialer implements DialAndSend function for mailer
type Dialer interface {
DialAndSend(m ...*gomail.Message) error
DialAndSend(ctx context.Context, m ...*gomail.Message) error
}

var getNewDialer func(host string, port int, username, password string) Dialer = func(host string, port int, username, password string) Dialer {
var NewDialer func(host string, port int, username, password string) Dialer = func(host string, port int, username, password string) Dialer {
return gomail.NewDialer(host, port, username, password)
}

func SendMail(conn EmailConn) error {
func SendMail(ctx context.Context, conn EmailConn) error {
mail := gomail.NewMessage()
mail.SetHeader("From", conn.SenderAddr)

Expand Down Expand Up @@ -61,6 +63,6 @@ func SendMail(conn EmailConn) error {
mail.Attach(attachment)
}
// Send Mail
dialer := getNewDialer(conn.ServerHost, conn.ServerPort, conn.Username, conn.Password)
return dialer.DialAndSend(mail)
dialer := NewDialer(conn.ServerHost, conn.ServerPort, conn.Username, conn.Password)
return dialer.DialAndSend(ctx, mail)
}
11 changes: 6 additions & 5 deletions internal/tasks/mail_test.go
@@ -1,26 +1,27 @@
package tasks

import (
"context"
"testing"

gomail "github.com/ory/mail/v3"
"github.com/stretchr/testify/assert"
"gopkg.in/gomail.v2"
)

type fakeDialer struct {
Dialer
}

func (d *fakeDialer) DialAndSend(m ...*gomail.Message) error {
func (d *fakeDialer) DialAndSend(ctx context.Context, m ...*gomail.Message) error {
return nil
}

func TestTaskSendMail(t *testing.T) {
assert.NotNil(t, getNewDialer("", 0, "", ""), "Default dialer should be created")
getNewDialer = func(host string, port int, username, password string) Dialer {
assert.NotNil(t, NewDialer("", 0, "", ""), "Default dialer should be created")
NewDialer = func(host string, port int, username, password string) Dialer {
return &fakeDialer{}
}
assert.NoError(t, SendMail(EmailConn{
assert.NoError(t, SendMail(context.Background(), EmailConn{
ServerHost: "smtp.example.com",
ServerPort: 587,
Username: "user",
Expand Down
3 changes: 1 addition & 2 deletions samples/Download.sql
Expand Up @@ -53,8 +53,7 @@ BEGIN

RAISE NOTICE 'Step 2 completed. Unacent task added';

-- Step 3. Import ASCII file to PostgreSQL table using "psql \copy"
-- Add PROGRAM task 'psql' to the chain
-- Step 3. Import ASCII file to PostgreSQL table using "CopyFromFile" built-in command
INSERT INTO timetable.task (parent_id, command_id)
VALUES (v_task_id, timetable.get_command_id ('CopyFromFile'))
RETURNING
Expand Down
3 changes: 2 additions & 1 deletion samples/Shell.sql
Expand Up @@ -33,7 +33,8 @@ BEGIN
"-p", "' || inet_server_port() || '",
"-d", "' || current_database() || '",
"-U", "' || current_user || '",
"-c", "SELECT now();"
"-c", "SELECT now();",
"-w"
]')::jsonb
);
END $$
Expand Down

0 comments on commit 16b9421

Please sign in to comment.