|
| 1 | +package rega |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "ccu-addon-mui-server/pkg/config" |
| 12 | +) |
| 13 | + |
| 14 | +func TestSanitizeRegaValue(t *testing.T) { |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + input string |
| 18 | + want string |
| 19 | + }{ |
| 20 | + {name: "boolean true", input: "true", want: "true"}, |
| 21 | + {name: "boolean false", input: "false", want: "false"}, |
| 22 | + {name: "integer", input: "-12", want: "-12"}, |
| 23 | + {name: "float", input: "12.5", want: "12.5"}, |
| 24 | + {name: "string quoted", input: "hello", want: "\"hello\""}, |
| 25 | + {name: "string escaped", input: "a\\b\"c", want: "\"a\\\\b\\\"c\""}, |
| 26 | + } |
| 27 | + |
| 28 | + for _, tt := range tests { |
| 29 | + t.Run(tt.name, func(t *testing.T) { |
| 30 | + if got := sanitizeRegaValue(tt.input); got != tt.want { |
| 31 | + t.Fatalf("sanitizeRegaValue(%q) = %q, want %q", tt.input, got, tt.want) |
| 32 | + } |
| 33 | + }) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func TestSetDatapointRejectsInvalidIdentifiers(t *testing.T) { |
| 38 | + client := &Client{} |
| 39 | + |
| 40 | + _, err := client.SetDatapoint("HmIP-RF", "abc\";DROP", "STATE", "1") |
| 41 | + if err == nil { |
| 42 | + t.Fatal("expected error for invalid identifier, got nil") |
| 43 | + } |
| 44 | + if !strings.Contains(err.Error(), "invalid identifier") { |
| 45 | + t.Fatalf("expected invalid identifier error, got %v", err) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func TestExecuteStripsXMLWrapper(t *testing.T) { |
| 50 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 51 | + if r.Method != http.MethodPost { |
| 52 | + t.Fatalf("expected POST, got %s", r.Method) |
| 53 | + } |
| 54 | + w.WriteHeader(http.StatusOK) |
| 55 | + _, _ = io.WriteString(w, "payload before xml<xml><anything/></xml>\n") |
| 56 | + })) |
| 57 | + defer ts.Close() |
| 58 | + |
| 59 | + client := &Client{ |
| 60 | + cfg: &config.Config{}, |
| 61 | + httpClient: ts.Client(), |
| 62 | + baseURL: ts.URL, |
| 63 | + } |
| 64 | + |
| 65 | + got, err := client.Execute("Write(\"x\")") |
| 66 | + if err != nil { |
| 67 | + t.Fatalf("Execute returned error: %v", err) |
| 68 | + } |
| 69 | + if got != "payload before xml" { |
| 70 | + t.Fatalf("expected wrapper to be removed, got %q", got) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestExecuteUsesBasicAuthWhenConfigured(t *testing.T) { |
| 75 | + const user = "alice" |
| 76 | + const pass = "secret" |
| 77 | + |
| 78 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 79 | + u, p, ok := r.BasicAuth() |
| 80 | + if !ok { |
| 81 | + t.Fatal("expected basic auth header") |
| 82 | + } |
| 83 | + if u != user || p != pass { |
| 84 | + t.Fatalf("unexpected credentials user=%q pass=%q", u, p) |
| 85 | + } |
| 86 | + w.WriteHeader(http.StatusOK) |
| 87 | + _, _ = io.WriteString(w, "ok") |
| 88 | + })) |
| 89 | + defer ts.Close() |
| 90 | + |
| 91 | + client := &Client{ |
| 92 | + cfg: &config.Config{CCUUser: user, CCUPass: pass}, |
| 93 | + httpClient: ts.Client(), |
| 94 | + baseURL: ts.URL, |
| 95 | + } |
| 96 | + |
| 97 | + got, err := client.Execute("Write(\"x\")") |
| 98 | + if err != nil { |
| 99 | + t.Fatalf("Execute returned error: %v", err) |
| 100 | + } |
| 101 | + if got != "ok" { |
| 102 | + t.Fatalf("expected ok, got %q", got) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func TestExecuteReturnsStatusError(t *testing.T) { |
| 107 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 108 | + w.WriteHeader(http.StatusBadGateway) |
| 109 | + })) |
| 110 | + defer ts.Close() |
| 111 | + |
| 112 | + client := &Client{ |
| 113 | + cfg: &config.Config{}, |
| 114 | + httpClient: ts.Client(), |
| 115 | + baseURL: ts.URL, |
| 116 | + } |
| 117 | + |
| 118 | + _, err := client.Execute("Write(\"x\")") |
| 119 | + if err == nil { |
| 120 | + t.Fatal("expected status error, got nil") |
| 121 | + } |
| 122 | + if !strings.Contains(err.Error(), fmt.Sprintf("status %d", http.StatusBadGateway)) { |
| 123 | + t.Fatalf("expected status code in error, got %v", err) |
| 124 | + } |
| 125 | +} |
0 commit comments