-
Notifications
You must be signed in to change notification settings - Fork 12
/
post_nuke_notifier_test.go
89 lines (68 loc) · 2.37 KB
/
post_nuke_notifier_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package keynuker
import (
"log"
"testing"
"time"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/google/go-github/github"
"github.com/stretchr/testify/assert"
"gopkg.in/mailgun/mailgun-go.v1"
)
func TestSendPostNukeNotifications(t *testing.T) {
fakeUserEmail := "[email protected]"
fakeAdminEmail := "[email protected]"
// Mailgun instance -- either live or mock depending on whether integration tests enabled
var mg mailgun.Mailgun
var err error
// Create fake inputs that include nuked key events
params := ParamsPostNukeNotifier{
NukedKeyEvents: []NukedKeyEvent{
{
NukedOn: time.Now(),
LeakedKeyEvent: LeakedKeyEvent{
GithubUser: &github.User{
Email: aws.String(fakeUserEmail),
},
GithubEvent: &github.Event{},
AccessKeyMetadata: FetchedAwsAccessKey{
AccessKeyId: aws.String("fake-aws-access-key"),
},
},
DeleteAccessKeyOutput: &iam.DeleteAccessKeyOutput{},
},
},
KeynukerAdminEmailCCAddress: fakeAdminEmail,
EmailFromAddress: fakeAdminEmail,
MailerParams: MailerParams{},
}
// Unless integration tests are enabled, use mock
mockTest := !IntegrationTestsEnabled()
if mockTest {
// Create mock mailgun and configure it to use that
mg = NewMockMailGun()
} else {
// Integration test instructions -- set env variables MAILERDOMAIN, MAILERAPIKEY, and MAILERPUBLICAPIKEY
mg, err = NewMailgunFromEnvironmentVariables()
assert.NoError(t, err, fmt.Sprintf("Unexpected error"))
fakeUserEmail = "[email protected]" // Replace these to receive emails
fakeAdminEmail = "[email protected]"
}
// Call post nuke notifier
result, err := SendPostNukeMockNotifications(mg, params)
assert.NoError(t, err, fmt.Sprintf("Unexpected error"))
if mockTest {
mockMailgun := mg.(*MockMailGun)
// Wait until mock mailgun is invoked
msg, err := mockMailgun.WaitForNextMessage(time.Second)
assert.NoError(t, err, fmt.Sprintf("Unexpected error"))
log.Printf("Mailgun message: %v", msg)
// Verify that the correct outputs are returned (should propagate checkpoints)
log.Printf("result: %v", result)
assert.EqualValues(t, len(result.NukedKeyEvents), len(params.NukedKeyEvents))
assert.EqualValues(t, result.NukedKeyEvents, params.NukedKeyEvents)
// Should have at least one delivery id
assert.True(t, len(result.DeliveryIds) >= 1)
}
}