|
1 | 1 | package client
|
2 | 2 |
|
3 |
| -import "testing" |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "os" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/labbsr0x/bindman-dns-webhook/src/types" |
| 10 | +) |
| 11 | + |
| 12 | +func initClient() (*DNSWebhookClient, *MockHTTPHelper) { |
| 13 | + env := "BINDMAN_DNS_MANAGER_ADDRESS" |
| 14 | + mockHelper := new(MockHTTPHelper) |
| 15 | + os.Setenv(env, "0.0.0.0") |
| 16 | + c, _ := New(mockHelper) |
| 17 | + |
| 18 | + return c, mockHelper |
| 19 | +} |
4 | 20 |
|
5 | 21 | func TestNew(t *testing.T) {
|
| 22 | + env := "BINDMAN_DNS_MANAGER_ADDRESS" |
| 23 | + mockHelper := new(MockHTTPHelper) |
| 24 | + os.Setenv(env, "0.0.0.0") |
| 25 | + _, err := New(mockHelper) |
| 26 | + if err != nil { |
| 27 | + t.Errorf("Expecting client.New to succeed. Got error instead: '%v'", err) |
| 28 | + } |
| 29 | + |
| 30 | + c, err := New(nil) |
| 31 | + if err == nil { |
| 32 | + t.Errorf("Expecting client.New to fail. Got success instead: '%v'", c) |
| 33 | + } |
| 34 | + |
| 35 | + os.Setenv(env, "") |
| 36 | + c, err = New(mockHelper) |
| 37 | + if err == nil { |
| 38 | + t.Errorf("Expecting client.New to fail. Got success instead: '%v'", c) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestGetRecords(t *testing.T) { |
| 43 | + c, mockHelper := initClient() |
| 44 | + expected := []types.DNSRecord{types.DNSRecord{}, types.DNSRecord{}} |
| 45 | + mockHelper.GetData, _ = json.Marshal(expected) |
| 46 | + |
| 47 | + records, err := c.GetRecords() |
| 48 | + if err != nil { |
| 49 | + t.Errorf("Expecting successfull execution of GetRecords. Got error instead: '%v'", err) |
| 50 | + } |
| 51 | + if len(records) != len(expected) { |
| 52 | + t.Errorf("Expecting the number of records to be exactly the ") |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestGetRecord(t *testing.T) { |
| 57 | + c, mockHelper := initClient() |
| 58 | + expected := types.DNSRecord{Name: "teste"} |
| 59 | + mockHelper.GetData, _ = json.Marshal(expected) |
| 60 | + |
| 61 | + record, err := c.GetRecord(expected.Name) |
| 62 | + if err != nil { |
| 63 | + t.Errorf("Expecting successfull execution of GetRecord. Got error instead: '%v'", err) |
| 64 | + } |
6 | 65 |
|
| 66 | + if record.Name != expected.Name { |
| 67 | + t.Errorf("Expecting the recovered record name to match exactly the expected record. Got '%v' instead", record.Name) |
| 68 | + } |
7 | 69 | }
|
8 | 70 |
|
9 | 71 | func TestAddRecord(t *testing.T) {
|
| 72 | + c, mockHelper := initClient() |
| 73 | + expectedRecord := types.DNSRecord{Name: "teste", Value: "0.0.0.0", Type: "A"} |
| 74 | + expetectedResult := true |
10 | 75 |
|
| 76 | + mockHelper.PostData, _ = json.Marshal(expetectedResult) |
| 77 | + result, err := c.AddRecord(expectedRecord.Name, expectedRecord.Type, expectedRecord.Value) |
| 78 | + if err != nil { |
| 79 | + t.Errorf("Expecting to successfully add the record. Got error instead: %v", err) |
| 80 | + } |
| 81 | + |
| 82 | + if result != expetectedResult { |
| 83 | + t.Errorf("Expecting to successfully add the record. Got failure instead.") |
| 84 | + } |
11 | 85 | }
|
12 | 86 |
|
13 | 87 | func TestRemoveRecord(t *testing.T) {
|
| 88 | + c, mockHelper := initClient() |
| 89 | + expetectedResult := true |
| 90 | + |
| 91 | + mockHelper.DeleteData, _ = json.Marshal(expetectedResult) |
| 92 | + result, err := c.RemoveRecord("teste") |
| 93 | + if err != nil { |
| 94 | + t.Errorf("Expecting to successfully add the record. Got error instead: %v", err) |
| 95 | + } |
14 | 96 |
|
| 97 | + if result != expetectedResult { |
| 98 | + t.Errorf("Expecting to successfully add the record. Got failure instead.") |
| 99 | + } |
15 | 100 | }
|
16 | 101 |
|
17 | 102 | func TestGetRecordAPI(t *testing.T) {
|
18 |
| - |
| 103 | + api := getRecordAPI("manager.test.com", "test.test.com") |
| 104 | + expected := "http://manager.test.com/records/test.test.com" |
| 105 | + if api != expected { |
| 106 | + t.Errorf("Expecting '%v'; Got '%v'", expected, api) |
| 107 | + } |
19 | 108 | }
|
20 | 109 |
|
21 | 110 | func TestGetAddress(t *testing.T) {
|
| 111 | + env := "BINDMAN_TEST_ADDRESS" |
| 112 | + os.Setenv(env, "test.com") |
| 113 | + |
| 114 | + addr, err := getAddress(env) |
| 115 | + if addr == "" || err != nil { |
| 116 | + t.Errorf("Expecting the getAddress func to succeed. Got err instead: '%v'", err) |
| 117 | + } |
| 118 | + |
| 119 | + os.Setenv(env, "http://test.com") |
| 120 | + addr, err = getAddress(env) |
| 121 | + if err == nil { |
| 122 | + t.Errorf("Expecting the getAddress func to return error. Got success instead: '%v'", addr) |
| 123 | + } |
| 124 | +} |
22 | 125 |
|
| 126 | +type MockHTTPHelper struct { |
| 127 | + PostData []byte |
| 128 | + GetData []byte |
| 129 | + DeleteData []byte |
| 130 | +} |
| 131 | + |
| 132 | +func (m *MockHTTPHelper) Post(url string, data []byte) (http.Response, []byte, error) { |
| 133 | + return http.Response{}, m.PostData, nil |
| 134 | +} |
| 135 | + |
| 136 | +func (m *MockHTTPHelper) Get(url string) (http.Response, []byte, error) { |
| 137 | + return http.Response{}, m.GetData, nil |
| 138 | +} |
| 139 | +func (m *MockHTTPHelper) Delete(url string) (http.Response, []byte, error) { |
| 140 | + return http.Response{}, m.DeleteData, nil |
23 | 141 | }
|
0 commit comments