Skip to content

Commit

Permalink
add tests for GetSystemInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
mircea-pavel-anton committed Oct 16, 2024
1 parent 0b74740 commit e621bae
Showing 1 changed file with 133 additions and 2 deletions.
135 changes: 133 additions & 2 deletions internal/mikrotik/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,41 @@
package mikrotik

import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)

var (
mockUsername = "testuser"
mockPassword = "testpass"
mockServerInfo = SystemInfo{
ArchitectureName: "arm64",
BadBlocks: "0.1",
BoardName: "RB5009UG+S+",
BuildTime: "2024-09-20 13:00:27",
CPU: "ARM64",
CPUCount: "4",
CPUFrequency: "1400",
CPULoad: "0",
FactorySoftware: "7.4.1",
FreeHDDSpace: "1019346944",
FreeMemory: "916791296",
Platform: "MikroTik",
TotalHDDSpace: "1073741824",
TotalMemory: "1073741824",
Uptime: "4d19h9m34s",
Version: "7.16 (stable)",
WriteSectSinceReboot: "5868",
WriteSectTotal: "131658",
}
)

func TestNewMikrotikClient(t *testing.T) {
config := &Config{
Host: "test-host",
Port: "443",
BaseUrl: "https://192.168.88.1:443",
Username: "admin",
Password: "password",
SkipTLSVerify: true,
Expand Down Expand Up @@ -39,3 +66,107 @@ func TestNewMikrotikClient(t *testing.T) {
t.Errorf("Expected InsecureSkipVerify to be true")
}
}

func TestGetSystemInfo(t *testing.T) { // Set up your mock server
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Validate the Basic Auth header
username, password, ok := r.BasicAuth()
if !ok {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
if username != mockUsername || password != mockPassword {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}

// Return dummy data
if r.URL.Path == "/rest/system/resource" {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(mockServerInfo)

Check failure on line 86 in internal/mikrotik/client_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint / lint

Error return value of `(*encoding/json.Encoder).Encode` is not checked (errcheck)
return
}

// 404 on anything else
http.NotFound(w, r)
}))
defer server.Close()

// Set up the client with the test server's URL
config := &Config{
BaseUrl: server.URL,
Username: mockUsername,
Password: mockPassword,
SkipTLSVerify: true,
}

client, err := NewMikrotikClient(config)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}

// Call GetSystemInfo
info, err := client.GetSystemInfo()
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}

// Verify the returned system info
if info.ArchitectureName != mockServerInfo.ArchitectureName {
t.Errorf("Expected ArchitectureName %s, got %s", mockServerInfo.ArchitectureName, info.ArchitectureName)
}
if info.Version != mockServerInfo.Version {
t.Errorf("Expected Version %s, got %s", mockServerInfo.Version, info.Version)
}
}

func TestGetSystemInfo_Unauthorized(t *testing.T) {
// Set up your mock server
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Validate the Basic Auth header
username, password, ok := r.BasicAuth()
if !ok {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
if username != mockUsername || password != mockPassword {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}

// Return dummy data for /rest/system/resource
if r.URL.Path == "/rest/system/resource" && r.Method == http.MethodGet {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(mockServerInfo)

Check failure on line 140 in internal/mikrotik/client_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint / lint

Error return value of `(*encoding/json.Encoder).Encode` is not checked (errcheck)
return
}

// Return 404 for any other path
http.NotFound(w, r)
}))
defer server.Close()

// Set up the client with incorrect credentials
config := &Config{
BaseUrl: server.URL + "/rest",
Username: "wronguser",
Password: "wrongpass",
SkipTLSVerify: true,
}

client, err := NewMikrotikClient(config)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}

// Call GetSystemInfo and expect an error
_, err = client.GetSystemInfo()
if err == nil {
t.Fatalf("Expected error due to unauthorized access, got none")
}

// Optionally, check that the error message contains expected text
if !strings.Contains(err.Error(), "request failed") {
t.Errorf("Expected error message to contain 'request failed', got %v", err)
}
}

0 comments on commit e621bae

Please sign in to comment.