|
| 1 | +package middleware |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/mirkobrombin/goup/internal/config" |
| 9 | + "golang.org/x/crypto/bcrypt" |
| 10 | +) |
| 11 | + |
| 12 | +func TestBasicAuthMiddleware(t *testing.T) { |
| 13 | + // Setup |
| 14 | + password := "secret" |
| 15 | + hash, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) |
| 16 | + config.GlobalConf = &config.GlobalConfig{ |
| 17 | + Account: config.AccountConfig{ |
| 18 | + Username: "admin", |
| 19 | + PasswordHash: string(hash), |
| 20 | + }, |
| 21 | + } |
| 22 | + |
| 23 | + nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 24 | + w.WriteHeader(http.StatusOK) |
| 25 | + }) |
| 26 | + |
| 27 | + middleware := BasicAuthMiddleware(nextHandler) |
| 28 | + |
| 29 | + tests := []struct { |
| 30 | + name string |
| 31 | + user string |
| 32 | + pass string |
| 33 | + expectedStatus int |
| 34 | + }{ |
| 35 | + {"ValidCredentials", "admin", "secret", http.StatusOK}, |
| 36 | + {"InvalidUsername", "wrong", "secret", http.StatusUnauthorized}, |
| 37 | + {"InvalidPassword", "admin", "wrong", http.StatusUnauthorized}, |
| 38 | + {"NoCredentials", "", "", http.StatusUnauthorized}, |
| 39 | + } |
| 40 | + |
| 41 | + for _, tt := range tests { |
| 42 | + t.Run(tt.name, func(t *testing.T) { |
| 43 | + req := httptest.NewRequest("GET", "/", nil) |
| 44 | + if tt.user != "" { |
| 45 | + req.SetBasicAuth(tt.user, tt.pass) |
| 46 | + } |
| 47 | + rec := httptest.NewRecorder() |
| 48 | + middleware.ServeHTTP(rec, req) |
| 49 | + |
| 50 | + if rec.Code != tt.expectedStatus { |
| 51 | + t.Errorf("expected status %v, got %v", tt.expectedStatus, rec.Code) |
| 52 | + } |
| 53 | + }) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestTokenAuthMiddleware(t *testing.T) { |
| 58 | + // Setup |
| 59 | + token := "my-secret-token" |
| 60 | + config.GlobalConf = &config.GlobalConfig{ |
| 61 | + Account: config.AccountConfig{ |
| 62 | + APIToken: token, |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 67 | + w.WriteHeader(http.StatusOK) |
| 68 | + }) |
| 69 | + |
| 70 | + middleware := TokenAuthMiddleware(nextHandler) |
| 71 | + |
| 72 | + tests := []struct { |
| 73 | + name string |
| 74 | + headers map[string]string |
| 75 | + expectedStatus int |
| 76 | + }{ |
| 77 | + {"ValidTokenHeader", map[string]string{"X-API-Token": token}, http.StatusOK}, |
| 78 | + {"ValidBearerToken", map[string]string{"Authorization": "Bearer " + token}, http.StatusOK}, |
| 79 | + {"InvalidToken", map[string]string{"X-API-Token": "wrong"}, http.StatusUnauthorized}, |
| 80 | + {"NoToken", map[string]string{}, http.StatusOK}, |
| 81 | + } |
| 82 | + |
| 83 | + tests = []struct { |
| 84 | + name string |
| 85 | + headers map[string]string |
| 86 | + expectedStatus int |
| 87 | + }{ |
| 88 | + {"ValidTokenHeader", map[string]string{"X-API-Token": token}, http.StatusOK}, |
| 89 | + {"ValidBearerToken", map[string]string{"Authorization": "Bearer " + token}, http.StatusOK}, |
| 90 | + {"InvalidToken", map[string]string{"X-API-Token": "wrong"}, http.StatusUnauthorized}, |
| 91 | + {"NoToken", map[string]string{}, http.StatusUnauthorized}, |
| 92 | + } |
| 93 | + |
| 94 | + for _, tt := range tests { |
| 95 | + t.Run(tt.name, func(t *testing.T) { |
| 96 | + req := httptest.NewRequest("GET", "/", nil) |
| 97 | + for k, v := range tt.headers { |
| 98 | + req.Header.Set(k, v) |
| 99 | + } |
| 100 | + rec := httptest.NewRecorder() |
| 101 | + middleware.ServeHTTP(rec, req) |
| 102 | + |
| 103 | + if rec.Code != tt.expectedStatus { |
| 104 | + t.Errorf("expected status %v, got %v", tt.expectedStatus, rec.Code) |
| 105 | + } |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestAuthSkippedIfUnconfigured(t *testing.T) { |
| 111 | + // Setup: Empty config |
| 112 | + config.GlobalConf = &config.GlobalConfig{} |
| 113 | + |
| 114 | + nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 115 | + w.WriteHeader(http.StatusOK) |
| 116 | + }) |
| 117 | + |
| 118 | + t.Run("BasicAuthSkipped", func(t *testing.T) { |
| 119 | + middleware := BasicAuthMiddleware(nextHandler) |
| 120 | + req := httptest.NewRequest("GET", "/", nil) |
| 121 | + rec := httptest.NewRecorder() |
| 122 | + middleware.ServeHTTP(rec, req) |
| 123 | + if rec.Code != http.StatusOK { |
| 124 | + t.Errorf("expected status %v, got %v", http.StatusOK, rec.Code) |
| 125 | + } |
| 126 | + }) |
| 127 | + |
| 128 | + t.Run("TokenAuthSkipped", func(t *testing.T) { |
| 129 | + middleware := TokenAuthMiddleware(nextHandler) |
| 130 | + req := httptest.NewRequest("GET", "/", nil) |
| 131 | + rec := httptest.NewRecorder() |
| 132 | + middleware.ServeHTTP(rec, req) |
| 133 | + if rec.Code != http.StatusOK { |
| 134 | + t.Errorf("expected status %v, got %v", http.StatusOK, rec.Code) |
| 135 | + } |
| 136 | + }) |
| 137 | +} |
0 commit comments