|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "io/ioutil" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "os" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/gaia-pipeline/gaia" |
| 13 | + "github.com/gaia-pipeline/gaia/store" |
| 14 | + hclog "github.com/hashicorp/go-hclog" |
| 15 | + "github.com/labstack/echo" |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | +) |
| 18 | + |
| 19 | +func TestPipelineGitLSRemote(t *testing.T) { |
| 20 | + dataDir, err := ioutil.TempDir("", "temp") |
| 21 | + if err != nil { |
| 22 | + t.Fatalf("error creating data dir %v", err.Error()) |
| 23 | + } |
| 24 | + |
| 25 | + defer func() { |
| 26 | + gaia.Cfg = nil |
| 27 | + os.RemoveAll(dataDir) |
| 28 | + }() |
| 29 | + |
| 30 | + gaia.Cfg = &gaia.Config{ |
| 31 | + Logger: hclog.NewNullLogger(), |
| 32 | + DataPath: dataDir, |
| 33 | + } |
| 34 | + |
| 35 | + dataStore := store.NewStore() |
| 36 | + assert.NoError(t, dataStore.Init()) |
| 37 | + |
| 38 | + e := echo.New() |
| 39 | + InitHandlers(e, dataStore, nil) |
| 40 | + |
| 41 | + t.Run("fails with invalid data", func(t *testing.T) { |
| 42 | + req := httptest.NewRequest(echo.POST, "/api/"+apiVersion+"/pipeline/gitlsremote", nil) |
| 43 | + req.Header.Set("Content-Type", "application/json") |
| 44 | + rec := httptest.NewRecorder() |
| 45 | + c := e.NewContext(req, rec) |
| 46 | + |
| 47 | + assert.NoError(t, PipelineGitLSRemote(c)) |
| 48 | + assert.Equal(t, http.StatusBadRequest, rec.Code) |
| 49 | + }) |
| 50 | + |
| 51 | + t.Run("fails with invalid access", func(t *testing.T) { |
| 52 | + repoURL := "https://example.com" |
| 53 | + body := map[string]string{ |
| 54 | + "url": repoURL, |
| 55 | + "username": "admin", |
| 56 | + "password": "admin", |
| 57 | + } |
| 58 | + bodyBytes, _ := json.Marshal(body) |
| 59 | + req := httptest.NewRequest(echo.POST, "/api/"+apiVersion+"/pipeline/gitlsremote", bytes.NewBuffer(bodyBytes)) |
| 60 | + req.Header.Set("Content-Type", "application/json") |
| 61 | + rec := httptest.NewRecorder() |
| 62 | + c := e.NewContext(req, rec) |
| 63 | + |
| 64 | + assert.NoError(t, PipelineGitLSRemote(c)) |
| 65 | + assert.Equal(t, http.StatusForbidden, rec.Code) |
| 66 | + }) |
| 67 | + |
| 68 | + t.Run("otherwise succeed", func(t *testing.T) { |
| 69 | + repoURL := "https://github.com/gaia-pipeline/gaia" |
| 70 | + body := map[string]string{ |
| 71 | + "url": repoURL, |
| 72 | + "username": "admin", |
| 73 | + "password": "admin", |
| 74 | + } |
| 75 | + bodyBytes, _ := json.Marshal(body) |
| 76 | + req := httptest.NewRequest(echo.POST, "/api/"+apiVersion+"/pipeline/gitlsremote", bytes.NewBuffer(bodyBytes)) |
| 77 | + req.Header.Set("Content-Type", "application/json") |
| 78 | + rec := httptest.NewRecorder() |
| 79 | + c := e.NewContext(req, rec) |
| 80 | + |
| 81 | + assert.NoError(t, PipelineGitLSRemote(c)) |
| 82 | + assert.Equal(t, http.StatusOK, rec.Code) |
| 83 | + }) |
| 84 | +} |
0 commit comments