Skip to content

Commit

Permalink
Merge pull request #31 from arnisoph/main_tests
Browse files Browse the repository at this point in the history
Increase test coverage in cmd/postisto/
  • Loading branch information
arnisoph authored Mar 21, 2020
2 parents c819c58 + e0db8c4 commit 878b0c7
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ config/**/*.yaml
config/**/*.yml
coverage.txt
test/data/configs/invalid-unreadable-file/
test/data/configs/valid/local_imap_server/TestStartApp/.local.*
vendor/
30 changes: 24 additions & 6 deletions cmd/postisto/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,21 @@ import (
var build string

func main() {
app := newApp()

if err := app.Run(os.Args); err != nil {
goLog.Fatalln("Failed to start app:", err)
}
}

func newApp() *cli.App {
var configPath string
var logLevel string
var logJSON bool
var pollInterval time.Duration
var onetime bool

app := &cli.App{
app := cli.App{
Name: "poŝtisto",
Usage: "quite okay mail-sorting",
Flags: []cli.Flag{
Expand Down Expand Up @@ -56,19 +65,24 @@ func main() {
EnvVars: []string{"POLL_INTERVAL"},
Destination: &pollInterval,
},
&cli.BoolFlag{
Name: "onetime",
Usage: "run filter only once and exit the program afterwards",
Value: false,
EnvVars: []string{"ONETIME"},
Destination: &onetime,
},
},
Action: func(c *cli.Context) error {
return startApp(c, configPath, logLevel, logJSON, pollInterval)
return runApp(configPath, logLevel, logJSON, pollInterval, onetime)
},
Version: build,
}

if err := app.Run(os.Args); err != nil {
goLog.Fatalln("Failed to start app:", err)
}
return &app
}

func startApp(c *cli.Context, configPath string, logLevel string, logJSON bool, pollInterval time.Duration) error {
func runApp(configPath string, logLevel string, logJSON bool, pollInterval time.Duration, onetime bool) error {

if err := log.InitWithConfig(logLevel, logJSON); err != nil {
return err
Expand Down Expand Up @@ -116,6 +130,10 @@ func startApp(c *cli.Context, configPath string, logLevel string, logJSON bool,
}
}

if onetime {
return nil
}

time.Sleep(pollInterval)
}
}
64 changes: 64 additions & 0 deletions cmd/postisto/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"fmt"
"github.com/arnisoph/postisto/pkg/server"
"github.com/arnisoph/postisto/test/integration"
"github.com/stretchr/testify/require"
"io/ioutil"
"testing"
)

func TestNewApp(t *testing.T) {
app := newApp()
require := require.New(t)

// ACTUAL TESTS BELOW
require.Equal("quite okay mail-sorting", app.Usage)
}

func TestRunApp(t *testing.T) {
require := require.New(t)

// Create local test IMAP server
testContainer := integration.NewTestContainer()
acc := integration.NewAccount(t, testContainer.IP, "test", "test", testContainer.Imap, true, false, true, nil, testContainer.Redis)

// Simulate new unsorted mails by uploading
for _, mailNum := range []int{1, 2, 3, 4} {
require.NotNil(acc)
require.NotNil(acc.Connection)
require.NotNil(*acc.InputMailbox)
require.Nil(acc.Connection.Upload(fmt.Sprintf("../../test/data/mails/log%v.txt", mailNum), *acc.InputMailbox, nil))
}

// Write exposed port to disk
portConfig :=
"accounts:\n" +
" local_imap_server:\n" +
" enable: true\n" +
" connection:\n" +
" server: localhost\n" +
" username: test\n" +
" password: test\n" +
fmt.Sprintf(" port: %v\n", testContainer.Imap) +
" cacertfile: ../../test/data/certs/ca.pem"
require.NoError(ioutil.WriteFile("../../test/data/configs/valid/local_imap_server/TestStartApp/.local.acc.yaml", []byte(portConfig), 0644))

// ACTUAL TESTS BELOW
require.EqualError(runApp("does-not exist", "debug", false, 42, true), "lstat does-not exist: no such file or directory")
require.NoError(runApp("../../test/data/configs/valid/local_imap_server/TestStartApp/", "debug", false, 42, true))

// Verify results
fetchedMails, err := acc.Connection.Search(*acc.InputMailbox, nil, []string{server.FlaggedFlag})
require.Nil(err)
require.Equal(0, len(fetchedMails), "Unexpected num of mails in mailbox %v", *acc.InputMailbox)

fetchedMails, err = acc.Connection.Search("MyTarget", nil, []string{server.FlaggedFlag})
require.Nil(err)
require.Equal(3, len(fetchedMails), "Unexpected num of mails in mailbox %v", "MyTarget")

fetchedMails, err = acc.Connection.Search("MailFilterTest-TestRegex", nil, []string{server.FlaggedFlag})
require.Nil(err)
require.Equal(1, len(fetchedMails), "Unexpected num of mails in mailbox %v", "MailFilterTest-TestRegex")
}
4 changes: 4 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func NewConfigFromFile(configPath string) (*Config, error) {
}

for _, file := range configFiles {
log.Debugw("Parsing config YAML file", "file", file)

fileCfg := new(Config)
yamlFile, err := ioutil.ReadFile(file)

Expand All @@ -60,6 +62,7 @@ func NewConfigFromFile(configPath string) (*Config, error) {
log.Errorw("Failed to parse YAML file", err, "file", file)
return nil, err
}
log.Debugw("Successfully parsed YAML file", "file", file, "parsedFile", string(yamlFile))

// Merge configs from files
if err := mergo.Merge(cfg, fileCfg, mergo.WithOverride, mergo.WithTypeCheck); err != nil {
Expand All @@ -68,6 +71,7 @@ func NewConfigFromFile(configPath string) (*Config, error) {
}
}

log.Debugw("Successfully parsed all YAML files, checking for validity now", "cfg", cfg)
newCfg, err := cfg.validate(passwords)
if err != nil {
return nil, err
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# vim: ts=2 sw=2 et

filters:
local_imap_server:
main:
commands:
move: MyTarget
rules:
- and:
- from:
- "@youth4work.com"
test_regex:
commands:
move: MailFilterTest-TestRegex
rules:
- and:
- from:
- '^.*@bigrock\.com.*$'
- delivered-to:
- cyberzonec.in
- content-type:
- ^(?:text/html|text/plain).*$

0 comments on commit 878b0c7

Please sign in to comment.