|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "database/sql" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + "net/http/httptest" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/DATA-DOG/go-sqlmock" |
| 14 | + "github.com/gorilla/mux" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | +func TestListConfigsHandler(t *testing.T) { |
| 19 | + t.Run("returns config list", func(t *testing.T) { |
| 20 | + server, mock := newMockServer(t) |
| 21 | + now := time.Now().UTC().Truncate(time.Second) |
| 22 | + |
| 23 | + rows := sqlmock.NewRows([]string{"id", "config_name", "registry_url", "config", "created_at", "updated_at"}). |
| 24 | + AddRow(1, "test-config", "http://harbor:8080", json.RawMessage(`{"app_config":{}}`), now, now). |
| 25 | + AddRow(2, "prod-config", "http://harbor:8080", json.RawMessage(`{"app_config":{}}`), now, now) |
| 26 | + mock.ExpectQuery("SELECT .+ FROM configs").WillReturnRows(rows) |
| 27 | + |
| 28 | + req := httptest.NewRequest(http.MethodGet, "/api/configs", nil) |
| 29 | + rr := httptest.NewRecorder() |
| 30 | + server.listConfigsHandler(rr, req) |
| 31 | + |
| 32 | + require.Equal(t, http.StatusOK, rr.Code) |
| 33 | + require.Contains(t, rr.Body.String(), "test-config") |
| 34 | + require.Contains(t, rr.Body.String(), "prod-config") |
| 35 | + require.NoError(t, mock.ExpectationsWereMet()) |
| 36 | + }) |
| 37 | + |
| 38 | + t.Run("empty list", func(t *testing.T) { |
| 39 | + server, mock := newMockServer(t) |
| 40 | + |
| 41 | + rows := sqlmock.NewRows([]string{"id", "config_name", "registry_url", "config", "created_at", "updated_at"}) |
| 42 | + mock.ExpectQuery("SELECT .+ FROM configs").WillReturnRows(rows) |
| 43 | + |
| 44 | + req := httptest.NewRequest(http.MethodGet, "/api/configs", nil) |
| 45 | + rr := httptest.NewRecorder() |
| 46 | + server.listConfigsHandler(rr, req) |
| 47 | + |
| 48 | + require.Equal(t, http.StatusOK, rr.Code) |
| 49 | + require.NoError(t, mock.ExpectationsWereMet()) |
| 50 | + }) |
| 51 | + |
| 52 | + t.Run("db error returns 500", func(t *testing.T) { |
| 53 | + server, mock := newMockServer(t) |
| 54 | + |
| 55 | + mock.ExpectQuery("SELECT .+ FROM configs").WillReturnError(fmt.Errorf("db error")) |
| 56 | + |
| 57 | + req := httptest.NewRequest(http.MethodGet, "/api/configs", nil) |
| 58 | + rr := httptest.NewRecorder() |
| 59 | + server.listConfigsHandler(rr, req) |
| 60 | + |
| 61 | + require.Equal(t, http.StatusInternalServerError, rr.Code) |
| 62 | + require.NoError(t, mock.ExpectationsWereMet()) |
| 63 | + }) |
| 64 | +} |
| 65 | + |
| 66 | +func TestGetConfigHandler(t *testing.T) { |
| 67 | + t.Run("found returns 200", func(t *testing.T) { |
| 68 | + server, mock := newMockServer(t) |
| 69 | + now := time.Now().UTC().Truncate(time.Second) |
| 70 | + |
| 71 | + rows := sqlmock.NewRows([]string{"id", "config_name", "registry_url", "config", "created_at", "updated_at"}). |
| 72 | + AddRow(1, "test-config", "http://harbor:8080", json.RawMessage(`{"app_config":{}}`), now, now) |
| 73 | + mock.ExpectQuery("SELECT .+ FROM configs WHERE config_name"). |
| 74 | + WithArgs("test-config"). |
| 75 | + WillReturnRows(rows) |
| 76 | + |
| 77 | + req := httptest.NewRequest(http.MethodGet, "/api/configs/test-config", nil) |
| 78 | + req = mux.SetURLVars(req, map[string]string{"config": "test-config"}) |
| 79 | + |
| 80 | + rr := httptest.NewRecorder() |
| 81 | + server.getConfigHandler(rr, req) |
| 82 | + |
| 83 | + require.Equal(t, http.StatusOK, rr.Code) |
| 84 | + require.Contains(t, rr.Body.String(), "test-config") |
| 85 | + require.NoError(t, mock.ExpectationsWereMet()) |
| 86 | + }) |
| 87 | + |
| 88 | + t.Run("not found returns 404", func(t *testing.T) { |
| 89 | + server, mock := newMockServer(t) |
| 90 | + |
| 91 | + mock.ExpectQuery("SELECT .+ FROM configs WHERE config_name"). |
| 92 | + WithArgs("nonexistent"). |
| 93 | + WillReturnError(sql.ErrNoRows) |
| 94 | + |
| 95 | + req := httptest.NewRequest(http.MethodGet, "/api/configs/nonexistent", nil) |
| 96 | + req = mux.SetURLVars(req, map[string]string{"config": "nonexistent"}) |
| 97 | + |
| 98 | + rr := httptest.NewRecorder() |
| 99 | + server.getConfigHandler(rr, req) |
| 100 | + |
| 101 | + require.Equal(t, http.StatusNotFound, rr.Code) |
| 102 | + require.NoError(t, mock.ExpectationsWereMet()) |
| 103 | + }) |
| 104 | +} |
| 105 | + |
| 106 | +func TestCreateConfigHandler_InvalidBody(t *testing.T) { |
| 107 | + server, _ := newMockServer(t) |
| 108 | + |
| 109 | + req := httptest.NewRequest(http.MethodPost, "/api/configs", bytes.NewReader([]byte("not json"))) |
| 110 | + req.Header.Set("Content-Type", "application/json") |
| 111 | + |
| 112 | + rr := httptest.NewRecorder() |
| 113 | + server.createConfigHandler(rr, req) |
| 114 | + |
| 115 | + require.Equal(t, http.StatusBadRequest, rr.Code) |
| 116 | +} |
| 117 | + |
| 118 | +func TestCreateConfigHandler_EmptyName(t *testing.T) { |
| 119 | + server, _ := newMockServer(t) |
| 120 | + |
| 121 | + body, _ := json.Marshal(map[string]string{"config_name": ""}) |
| 122 | + req := httptest.NewRequest(http.MethodPost, "/api/configs", bytes.NewReader(body)) |
| 123 | + req.Header.Set("Content-Type", "application/json") |
| 124 | + |
| 125 | + rr := httptest.NewRecorder() |
| 126 | + server.createConfigHandler(rr, req) |
| 127 | + |
| 128 | + require.Equal(t, http.StatusBadRequest, rr.Code) |
| 129 | +} |
| 130 | + |
| 131 | +func TestDeleteConfigHandler_NotFound(t *testing.T) { |
| 132 | + server, mock := newMockServer(t) |
| 133 | + |
| 134 | + mock.ExpectQuery("SELECT .+ FROM configs WHERE config_name"). |
| 135 | + WithArgs("nonexistent"). |
| 136 | + WillReturnError(fmt.Errorf("db error")) |
| 137 | + |
| 138 | + req := httptest.NewRequest(http.MethodDelete, "/api/configs/nonexistent", nil) |
| 139 | + req = mux.SetURLVars(req, map[string]string{"config": "nonexistent"}) |
| 140 | + |
| 141 | + rr := httptest.NewRecorder() |
| 142 | + server.deleteConfigHandler(rr, req) |
| 143 | + |
| 144 | + require.Equal(t, http.StatusInternalServerError, rr.Code) |
| 145 | + require.NoError(t, mock.ExpectationsWereMet()) |
| 146 | +} |
| 147 | + |
| 148 | +func TestDeleteConfigHandler_ConfigInUse(t *testing.T) { |
| 149 | + server, mock := newMockServer(t) |
| 150 | + now := time.Now().UTC().Truncate(time.Second) |
| 151 | + |
| 152 | + mock.ExpectQuery("SELECT .+ FROM configs WHERE config_name"). |
| 153 | + WithArgs("used-config"). |
| 154 | + WillReturnRows(sqlmock.NewRows([]string{"id", "config_name", "registry_url", "config", "created_at", "updated_at"}). |
| 155 | + AddRow(1, "used-config", "http://harbor:8080", json.RawMessage(`{}`), now, now)) |
| 156 | + |
| 157 | + mock.ExpectQuery("SELECT .+ FROM satellite_configs"). |
| 158 | + WithArgs(int32(1)). |
| 159 | + WillReturnRows(sqlmock.NewRows([]string{"satellite_id", "config_id"}). |
| 160 | + AddRow(1, 1)) |
| 161 | + |
| 162 | + req := httptest.NewRequest(http.MethodDelete, "/api/configs/used-config", nil) |
| 163 | + req = mux.SetURLVars(req, map[string]string{"config": "used-config"}) |
| 164 | + |
| 165 | + rr := httptest.NewRecorder() |
| 166 | + server.deleteConfigHandler(rr, req) |
| 167 | + |
| 168 | + require.Equal(t, http.StatusBadRequest, rr.Code) |
| 169 | + require.NoError(t, mock.ExpectationsWereMet()) |
| 170 | +} |
0 commit comments