Skip to content

Commit

Permalink
Add notifier tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanprodan committed Aug 12, 2019
1 parent 040dbb8 commit 0a418eb
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
30 changes: 30 additions & 0 deletions pkg/notifier/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package notifier

import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)

func Test_postMessage(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
var payload = make(map[string]string)
err = json.Unmarshal(b, &payload)

if payload["status"] != "success" {
t.Fatal("wrong payload")
}
}))
defer ts.Close()

err := postMessage(ts.URL, map[string]string{"status": "success"})
if err != nil {
t.Fatal(err)
}
}
35 changes: 35 additions & 0 deletions pkg/notifier/slack_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package notifier

import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)

func TestSlack_Post(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
var payload = SlackPayload{}
err = json.Unmarshal(b, &payload)

if payload.Attachments[0].AuthorName != "podinfo.test" {
t.Fatal("wrong author name")
}
}))
defer ts.Close()

slack, err := NewSlack(ts.URL, "test", "test")
if err != nil {
t.Fatal(err)
}

err = slack.Post("podinfo", "test", "test", nil, true)
if err != nil {
t.Fatal(err)
}
}
35 changes: 35 additions & 0 deletions pkg/notifier/teams_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package notifier

import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)

func TestTeams_Post(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
var payload = MSTeamsPayload{}
err = json.Unmarshal(b, &payload)

if payload.Sections[0].ActivitySubtitle != "podinfo.test" {
t.Fatal("wrong activity subtitle")
}
}))
defer ts.Close()

teams, err := NewMSTeams(ts.URL)
if err != nil {
t.Fatal(err)
}

err = teams.Post("podinfo", "test", "test", nil, true)
if err != nil {
t.Fatal(err)
}
}

0 comments on commit 0a418eb

Please sign in to comment.