mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 00:45:19 +00:00
2a532ede94
#11266 PS: I first attempted a serialization trick by introducing a new `appConfigResponse` and implementing `json.Marshal` to exclude these fields but it was too hacky and hard to maintain moving forward, so I'm bitting the bullet now. Happy to hear other ideas. - [X] Changes file added for user-visible changes in `changes/` or `orbit/changes/`. See [Changes files](https://fleetdm.com/docs/contributing/committing-changes#changes-files) for more information. - ~[ ] Documented any API changes (docs/Using-Fleet/REST-API.md or docs/Contributing/API-for-contributors.md)~ - ~[ ] Documented any permissions changes~ - ~[ ] Input data is properly validated, `SELECT *` is avoided, SQL injection is prevented (using placeholders for values in statements)~ - ~[ ] Added support on fleet's osquery simulator `cmd/osquery-perf` for new osquery data ingestion features.~ - [X] Added/updated tests - [X] Manual QA for all new/changed functionality - ~For Orbit and Fleet Desktop changes:~ - ~[ ] Manual QA must be performed in the three main OSs, macOS, Windows and Linux.~ - ~[ ] Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).~
150 lines
3.3 KiB
Go
150 lines
3.3 KiB
Go
package mail
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ses"
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func Test_getFromSES(t *testing.T) {
|
|
type args struct {
|
|
e fleet.Email
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want string
|
|
wantErr assert.ErrorAssertionFunc
|
|
}{
|
|
{
|
|
name: "should return properly formatted SMTP from for use in SES",
|
|
args: args{e: fleet.Email{
|
|
ServerURL: "https://foobar.fleetdm.com",
|
|
}},
|
|
want: "From: do-not-reply@foobar.fleetdm.com\r\n",
|
|
wantErr: assert.NoError,
|
|
},
|
|
{
|
|
name: "should error when we fail to parse fleet server url",
|
|
args: args{e: fleet.Email{
|
|
ServerURL: "not-a-url",
|
|
}},
|
|
want: "",
|
|
wantErr: assert.Error,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := getFromSES(tt.args.e)
|
|
if !tt.wantErr(t, err, fmt.Sprintf("getFromSES(%v)", tt.args.e)) {
|
|
return
|
|
}
|
|
assert.Equalf(t, tt.want, got, "getFromSES(%v)", tt.args.e)
|
|
})
|
|
}
|
|
}
|
|
|
|
type mockSESSender struct {
|
|
shouldErr bool
|
|
}
|
|
|
|
func (m mockSESSender) SendRawEmail(input *ses.SendRawEmailInput) (*ses.SendRawEmailOutput, error) {
|
|
if m.shouldErr {
|
|
return nil, errors.New("some error")
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func Test_sesSender_SendEmail(t *testing.T) {
|
|
type fields struct {
|
|
client fleetSESSender
|
|
sourceArn string
|
|
}
|
|
type args struct {
|
|
e fleet.Email
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
fields fields
|
|
args args
|
|
wantErr assert.ErrorAssertionFunc
|
|
}{
|
|
{
|
|
name: "should send email",
|
|
fields: fields{
|
|
client: mockSESSender{shouldErr: false},
|
|
sourceArn: "foo",
|
|
},
|
|
args: args{e: fleet.Email{
|
|
Subject: "Hello from Fleet!",
|
|
To: []string{"foouser@fleetdm.com"},
|
|
ServerURL: "https://foobar.fleetdm.com",
|
|
Mailer: &SMTPTestMailer{
|
|
BaseURL: "https://localhost:8080",
|
|
},
|
|
}},
|
|
wantErr: assert.NoError,
|
|
},
|
|
{
|
|
name: "should error when email config is nil",
|
|
fields: fields{
|
|
client: mockSESSender{shouldErr: false},
|
|
sourceArn: "foo",
|
|
},
|
|
args: args{e: fleet.Email{
|
|
Subject: "Hello from Fleet!",
|
|
To: []string{"foouser@fleetdm.com"},
|
|
Mailer: &SMTPTestMailer{
|
|
BaseURL: "https://localhost:8080",
|
|
},
|
|
}},
|
|
wantErr: assert.Error,
|
|
},
|
|
{
|
|
name: "should error when ses client is nil",
|
|
fields: fields{
|
|
client: nil,
|
|
sourceArn: "foo",
|
|
},
|
|
args: args{e: fleet.Email{
|
|
Subject: "Hello from Fleet!",
|
|
To: []string{"foouser@fleetdm.com"},
|
|
ServerURL: "https://foobar.fleetdm.com",
|
|
Mailer: &SMTPTestMailer{
|
|
BaseURL: "https://localhost:8080",
|
|
},
|
|
}},
|
|
wantErr: assert.Error,
|
|
},
|
|
{
|
|
name: "should error when ses client returns an error",
|
|
fields: fields{
|
|
client: mockSESSender{shouldErr: true},
|
|
sourceArn: "foo",
|
|
},
|
|
args: args{e: fleet.Email{
|
|
Subject: "Hello from Fleet!",
|
|
To: []string{"foouser@fleetdm.com"},
|
|
ServerURL: "https://foobar.fleetdm.com",
|
|
Mailer: &SMTPTestMailer{
|
|
BaseURL: "https://localhost:8080",
|
|
},
|
|
}},
|
|
wantErr: assert.Error,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
s := &sesSender{
|
|
client: tt.fields.client,
|
|
sourceArn: tt.fields.sourceArn,
|
|
}
|
|
tt.wantErr(t, s.SendEmail(tt.args.e), fmt.Sprintf("SendEmail(%v)", tt.args.e))
|
|
})
|
|
}
|
|
}
|