diff options
Diffstat (limited to 'api/http/git_file_handlers_test.go')
-rw-r--r-- | api/http/git_file_handlers_test.go | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/api/http/git_file_handlers_test.go b/api/http/git_file_handlers_test.go new file mode 100644 index 00000000..81d97d61 --- /dev/null +++ b/api/http/git_file_handlers_test.go @@ -0,0 +1,91 @@ +package http + +import ( + "bytes" + "image" + "image/png" + "mime/multipart" + "net/http" + "net/http/httptest" + "testing" + + "github.com/gorilla/mux" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/MichaelMure/git-bug/api/auth" + "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/repository" +) + +func TestGitFileHandlers(t *testing.T) { + repo := repository.CreateTestRepo(false) + defer repository.CleanupTestRepos(repo) + + mrc := cache.NewMultiRepoCache() + repoCache, err := mrc.RegisterDefaultRepository(repo) + require.NoError(t, err) + + author, err := repoCache.NewIdentity("test identity", "test@test.org") + require.NoError(t, err) + + err = repoCache.SetUserIdentity(author) + require.NoError(t, err) + + // UPLOAD + + uploadHandler := NewGitUploadFileHandler(mrc) + + img := image.NewNRGBA(image.Rect(0, 0, 50, 50)) + data := &bytes.Buffer{} + err = png.Encode(data, img) + require.NoError(t, err) + + body := &bytes.Buffer{} + writer := multipart.NewWriter(body) + part, err := writer.CreateFormFile("uploadfile", "noname") + assert.NoError(t, err) + + _, err = part.Write(data.Bytes()) + assert.NoError(t, err) + + err = writer.Close() + assert.NoError(t, err) + + w := httptest.NewRecorder() + r, _ := http.NewRequest("GET", "/", body) + r.Header.Add("Content-Type", writer.FormDataContentType()) + + // Simulate auth + r = r.WithContext(auth.CtxWithUser(r.Context(), author.Id())) + + // Handler's params + r = mux.SetURLVars(r, map[string]string{ + "repo": "", + }) + + uploadHandler.ServeHTTP(w, r) + + assert.Equal(t, http.StatusOK, w.Code) + assert.Equal(t, `{"hash":"3426a1488292d8f3f3c59ca679681336542b986f"}`, w.Body.String()) + // DOWNLOAD + + downloadHandler := NewGitFileHandler(mrc) + + w = httptest.NewRecorder() + r, _ = http.NewRequest("GET", "/", nil) + + // Simulate auth + r = r.WithContext(auth.CtxWithUser(r.Context(), author.Id())) + + // Handler's params + r = mux.SetURLVars(r, map[string]string{ + "repo": "", + "hash": "3426a1488292d8f3f3c59ca679681336542b986f", + }) + + downloadHandler.ServeHTTP(w, r) + assert.Equal(t, http.StatusOK, w.Code) + + assert.Equal(t, data.Bytes(), w.Body.Bytes()) +} |