|
| 1 | +/* |
| 2 | +Copyright 2020 The Flux authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package notifier |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "io" |
| 22 | + "net/http" |
| 23 | + "net/http/httptest" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | +) |
| 28 | + |
| 29 | +func TestRingCentral_NewRingCentral(t *testing.T) { |
| 30 | + _, err := NewRingCentral("invalid-url", "") |
| 31 | + require.Error(t, err) |
| 32 | + |
| 33 | + ringCentral, err := NewRingCentral("http://localhost", "") |
| 34 | + require.NoError(t, err) |
| 35 | + require.Equal(t, "http://localhost", ringCentral.URL) |
| 36 | +} |
| 37 | + |
| 38 | +func TestRingCentral_Post(t *testing.T) { |
| 39 | + fields := []Field{ |
| 40 | + {Name: "name1", Value: "value1"}, |
| 41 | + {Name: "name2", Value: "value2"}, |
| 42 | + } |
| 43 | + |
| 44 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 45 | + b, err := io.ReadAll(r.Body) |
| 46 | + require.NoError(t, err) |
| 47 | + |
| 48 | + var payload = RingCentralPayload{} |
| 49 | + err = json.Unmarshal(b, &payload) |
| 50 | + require.NoError(t, err) |
| 51 | + require.Equal(t, "podinfo.test", payload.Activity) |
| 52 | + require.Equal(t, len(fields)+1, len(payload.Attachments[0].Body)) |
| 53 | + require.Equal(t, "http://adaptivecards.io/schemas/adaptive-card.json", payload.Attachments[0].Schema) |
| 54 | + require.Equal(t, "AdaptiveCard", payload.Attachments[0].Type) |
| 55 | + require.Equal(t, "1.0", payload.Attachments[0].Version) |
| 56 | + })) |
| 57 | + defer ts.Close() |
| 58 | + |
| 59 | + ringCentral, err := NewRingCentral(ts.URL, "") |
| 60 | + require.NoError(t, err) |
| 61 | + |
| 62 | + err = ringCentral.Post("podinfo", "test", "test", fields, "info") |
| 63 | + require.NoError(t, err) |
| 64 | + err = ringCentral.Post("podinfo", "test", "test", fields, "error") |
| 65 | + require.NoError(t, err) |
| 66 | + err = ringCentral.Post("podinfo", "test", "test", fields, "warn") |
| 67 | + require.NoError(t, err) |
| 68 | + |
| 69 | +} |
0 commit comments