-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqs_test.go
More file actions
97 lines (84 loc) · 2.33 KB
/
sqs_test.go
File metadata and controls
97 lines (84 loc) · 2.33 KB
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
90
91
92
93
94
95
96
97
package main
import (
"bytes"
"encoding/json"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sns"
"io/ioutil"
"os"
"testing"
)
func TestReadSqs(t *testing.T) {
queue := os.Getenv("AWS_SQS_QUEUE")
topic := os.Getenv("AWS_SNS_TOPIC")
content, err := ioutil.ReadFile("tests/valid-hook.req")
if err != nil {
t.Fatalf("%s", err.Error())
}
err = pushSns(bytes.NewBuffer(content).String(), topic)
if err != nil {
t.Fatalf("%s", err.Error())
}
message, err := ReadSqs(queue)
if err != nil {
t.Fatalf("%s", err.Error())
}
DeleteMessage(queue, message.ReceiptHandle)
messageObject := &HookMessage{}
err = json.Unmarshal(bytes.NewBufferString(*message.Body).Bytes(), messageObject)
if err != nil {
t.Errorf("message: %v", *message.Body)
t.Fatalf("%s", err.Error())
}
if _, ok := messageObject.MessageAttributes["X-Github-Event"]; ok == false {
t.Fatalf("Attribute not set")
}
if messageObject.MessageAttributes["X-Github-Event"].Value == "" {
t.Fatalf("Event value is not set")
}
}
func TestReadHookFromSqs(t *testing.T) {
queue := os.Getenv("AWS_SQS_QUEUE")
topic := os.Getenv("AWS_SNS_TOPIC")
content, err := ioutil.ReadFile("tests/valid-hook.req")
if err != nil {
t.Fatalf("%s", err.Error())
}
err = pushSns(bytes.NewBuffer(content).String(), topic)
if err != nil {
t.Fatalf("%s", err.Error())
}
messageObject, err := ReadHookFromSqs(queue)
if err != nil {
t.Fatalf("%s", err.Error())
}
DeleteMessage(messageObject.QueueUrl, messageObject.ReceiptHandle)
if _, ok := messageObject.MessageAttributes["X-Github-Event"]; ok == false {
t.Fatalf("Attribute not set")
}
if messageObject.MessageAttributes["X-Github-Event"].Value == "" {
t.Fatalf("Event value is not set")
}
}
func pushSns(content string, topic string) error {
params := &sns.PublishInput{
Subject: aws.String("GitHub Web Hook"),
Message: aws.String(content),
TopicArn: aws.String(topic),
}
attr := map[string]*sns.MessageAttributeValue{
"X-Github-Event": {
DataType: aws.String("String"),
StringValue: aws.String("push"),
},
"X-Hub-Signature": {
DataType: aws.String("String"),
StringValue: aws.String("sha1=0000000000000000000000000000000000000000"),
},
}
params.SetMessageAttributes(attr)
session := GetSession()
snsService := sns.New(session)
_, err := snsService.Publish(params)
return err
}