forked from linkedin/Iris-message-processor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_test.go
More file actions
111 lines (95 loc) · 2.56 KB
/
api_test.go
File metadata and controls
111 lines (95 loc) · 2.56 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"sync"
"testing"
"time"
)
// HMACPost generic POST with HMAC authentication for use in testing server endpoints
func HMACPost(url string, jsonBody []byte) (respBody []byte, statusCode int, err error) {
//create http client
httpClient := &http.Client{
Timeout: time.Second * 5,
}
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(jsonBody))
if err != nil {
return
}
req.Header.Set("Content-Type", "application/json")
auth, _, err := AuthHeader("POST", "/", jsonBody, "iris", []byte("iriskey"))
if err != nil {
return
}
req.Header.Add("Authorization", auth)
resp, err := httpClient.Do(req)
if err != nil {
return
}
statusCode = resp.StatusCode
defer resp.Body.Close()
respBody, err = ioutil.ReadAll(resp.Body)
if err != nil {
return
}
return
}
func TestpostNotification(t *testing.T) {
logger := NewLogger("tests", "", false)
defer logger.RedirectStdLogger()()
defer logger.Close()
buildMessagePath := filepath.Join("testData", "message.json")
buildMessageData, _ := ioutil.ReadFile(buildMessagePath)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.URL.Path, "/notification") {
postNotification(w, r, nil)
} else if strings.Contains(r.URL.Path, "/build_message") {
w.Write(buildMessageData)
}
}))
defer ts.Close()
// configure authchecker
var quitChan chan int
cfg := Config{
IrisDryRun: false,
MetricsDryRun: false,
DebugAuth: false,
}
var dummyWg sync.WaitGroup
irisClient := NewIrisClient(ts.URL, "iris-message-processor", "dummykey", nil, &cfg, logger)
authChecker = NewAuthChecker(irisClient, &cfg, logger, quitChan, &dummyWg, nil)
authChecker.appAuthMap["iris"] = "iriskey"
payload := Notification{
Role: "mailing-list",
Target: "monitoring-visualization-and-alerting",
Subject: "subject",
Template: "iris-message-processor-template",
Context: map[string]interface{}{
"var": "successfully rendered the template variable",
},
Priority: "High",
PriorityID: 17,
Application: "iris-message-processor",
}
b, err := json.Marshal(payload)
if err != nil {
t.Fatalf("Failed to marshal json with error %v", err)
}
body, statusCode, err := HMACPost(ts.URL, b)
if err != nil {
t.Fatalf("Failed to post with error %v", err)
}
var got, want int
got = statusCode
want = 200
if got != want {
fmt.Printf(string(body))
t.Fatalf("response status code = %d; want %d", got, want)
}
}