Openstatus www.openstatus.dev
6
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 191 lines 4.6 kB view raw
1package handlers_test 2 3import ( 4 "encoding/json" 5 "reflect" 6 "testing" 7 8 // Adjust the import path if necessary to point to the correct package 9 "github.com/openstatushq/openstatus/apps/checker/checker" 10 "github.com/openstatushq/openstatus/apps/checker/handlers" 11) 12 13// Mock DNSResult struct to match the expected input for FormatDNSResult. 14// If the real struct is in another package, import it accordingly. 15type DNSResult struct { 16 A []string 17 AAAA []string 18 CNAME string 19 MX []string 20 NS []string 21 TXT []string 22} 23 24func TestFormatDNSResult(t *testing.T) { 25 tests := []struct { 26 name string 27 input DNSResult 28 expected map[string][]string 29 }{ 30 { 31 name: "All fields populated", 32 input: DNSResult{ 33 A: []string{"1.2.3.4", "5.6.7.8"}, 34 AAAA: []string{"::1", "2001:db8::1"}, 35 CNAME: "example.com", 36 MX: []string{"mx1.example.com", "mx2.example.com"}, 37 NS: []string{"ns1.example.com", "ns2.example.com"}, 38 TXT: []string{"v=spf1", "google-site-verification=abc"}, 39 }, 40 expected: map[string][]string{ 41 "A": {"1.2.3.4", "5.6.7.8"}, 42 "AAAA": {"::1", "2001:db8::1"}, 43 "CNAME": {"example.com"}, 44 "MX": {"mx1.example.com", "mx2.example.com"}, 45 "NS": {"ns1.example.com", "ns2.example.com"}, 46 "TXT": {"v=spf1", "google-site-verification=abc"}, 47 }, 48 }, 49 { 50 name: "Empty fields", 51 input: DNSResult{ 52 A: []string{}, 53 AAAA: []string{}, 54 CNAME: "", 55 MX: []string{}, 56 NS: []string{}, 57 TXT: []string{}, 58 }, 59 expected: map[string][]string{ 60 "A": {}, 61 "AAAA": {}, 62 "CNAME": {""}, 63 "MX": {}, 64 "NS": {}, 65 "TXT": {}, 66 }, 67 }, 68 { 69 name: "Single values", 70 input: DNSResult{ 71 A: []string{"8.8.8.8"}, 72 AAAA: []string{"fe80::1"}, 73 CNAME: "single.example.com", 74 MX: []string{"mx.single.com"}, 75 NS: []string{"ns.single.com"}, 76 TXT: []string{"single-txt"}, 77 }, 78 expected: map[string][]string{ 79 "A": {"8.8.8.8"}, 80 "AAAA": {"fe80::1"}, 81 "CNAME": {"single.example.com"}, 82 "MX": {"mx.single.com"}, 83 "NS": {"ns.single.com"}, 84 "TXT": {"single-txt"}, 85 }, 86 }, 87 } 88 89 for _, tt := range tests { 90 t.Run(tt.name, func(t *testing.T) { 91 got := handlers.FormatDNSResult(&checker.DnsResponse{ 92 A: tt.input.A, 93 AAAA: tt.input.AAAA, 94 CNAME: tt.input.CNAME, 95 MX: tt.input.MX, 96 NS: tt.input.NS, 97 TXT: tt.input.TXT, 98 }) 99 if !reflect.DeepEqual(got, tt.expected) { 100 t.Errorf("FormatDNSResult() = %v, want %v", got, tt.expected) 101 } 102 }) 103 } 104} 105 106func TestEvaluateDNSAssertions(t *testing.T) { 107 type args struct { 108 rawAssertions []json.RawMessage 109 response *checker.DnsResponse 110 } 111 tests := []struct { 112 name string 113 args args 114 wantSuccess bool 115 wantErr bool 116 }{ 117 { 118 name: "A record matches", 119 args: args{ 120 rawAssertions: []json.RawMessage{ 121 json.RawMessage(`{"key":"A","compare":"eq","target":"1.2.3.4"}`), 122 }, 123 response: &checker.DnsResponse{ 124 A: []string{"1.2.3.4", "5.6.7.8"}, 125 }, 126 }, 127 wantSuccess: true, 128 wantErr: false, 129 }, 130 { 131 name: "CNAME does not match", 132 args: args{ 133 rawAssertions: []json.RawMessage{ 134 json.RawMessage(`{"key":"CNAME","compare":"eq","target":"not-example.com"}`), 135 }, 136 response: &checker.DnsResponse{ 137 CNAME: "example.com", 138 }, 139 }, 140 wantSuccess: false, 141 wantErr: false, 142 }, 143 { 144 name: "CNAME Contains", 145 args: args{ 146 rawAssertions: []json.RawMessage{ 147 json.RawMessage(`{"version":"v1","type":"dnsRecord","key":"CNAME","compare":"contains","target":"openstatus.dev"}`), 148 }, 149 response: &checker.DnsResponse{ 150 CNAME: "openstatus.dev.", 151 }, 152 }, 153 wantSuccess: true, 154 wantErr: false, 155 }, 156 { 157 name: "Unknown record type", 158 args: args{ 159 rawAssertions: []json.RawMessage{ 160 json.RawMessage(`{"key":"FOO","compare":"eq","target":"bar"}`), 161 }, 162 response: &checker.DnsResponse{}, 163 }, 164 wantSuccess: false, 165 wantErr: true, 166 }, 167 { 168 name: "Invalid assertion JSON", 169 args: args{ 170 rawAssertions: []json.RawMessage{ 171 json.RawMessage(`not a json`), 172 }, 173 response: &checker.DnsResponse{}, 174 }, 175 wantSuccess: false, 176 wantErr: true, 177 }, 178 } 179 180 for _, tt := range tests { 181 t.Run(tt.name, func(t *testing.T) { 182 got, err := handlers.EvaluateDNSAssertions(tt.args.rawAssertions, tt.args.response) 183 if (err != nil) != tt.wantErr { 184 t.Errorf("error = %v, wantErr %v", err, tt.wantErr) 185 } 186 if got != tt.wantSuccess { 187 t.Errorf("EvaluateDNSAssertions() = %v, want %v", got, tt.wantSuccess) 188 } 189 }) 190 } 191}