diff options
author | Earl Warren <109468362+earl-warren@users.noreply.github.com> | 2023-08-24 12:36:10 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-24 10:36:10 +0000 |
commit | a9ce570298d4541bc1b5598dc080d9e4541de17b (patch) | |
tree | 41be5a2f94a0582c4d56a1ede7a7d57b820b91c8 /tests/integration/api_releases_test.go | |
parent | b21b63c61ac1038a1597acee45084896f5e39a3a (diff) | |
download | gitea-a9ce570298d4541bc1b5598dc080d9e4541de17b.tar.gz gitea-a9ce570298d4541bc1b5598dc080d9e4541de17b.zip |
add Upload URL to release API (#26663)
- Resolves https://codeberg.org/forgejo/forgejo/issues/580
- Return a `upload_field` to any release API response, which points to
the API URL for uploading new assets.
- Adds unit test.
- Adds integration testing to verify URL is returned correctly and that
upload endpoint actually works
---------
Co-authored-by: Gusted <postmaster@gusted.xyz>
Diffstat (limited to 'tests/integration/api_releases_test.go')
-rw-r--r-- | tests/integration/api_releases_test.go | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/tests/integration/api_releases_test.go b/tests/integration/api_releases_test.go index 7f43939083..526842d5ac 100644 --- a/tests/integration/api_releases_test.go +++ b/tests/integration/api_releases_test.go @@ -4,9 +4,13 @@ package integration import ( + "bytes" "fmt" + "io" + "mime/multipart" "net/http" "net/url" + "strings" "testing" auth_model "code.gitea.io/gitea/models/auth" @@ -38,12 +42,15 @@ func TestAPIListReleases(t *testing.T) { case 1: assert.False(t, release.IsDraft) assert.False(t, release.IsPrerelease) + assert.True(t, strings.HasSuffix(release.UploadURL, "/api/v1/repos/user2/repo1/releases/1/assets"), release.UploadURL) case 4: assert.True(t, release.IsDraft) assert.False(t, release.IsPrerelease) + assert.True(t, strings.HasSuffix(release.UploadURL, "/api/v1/repos/user2/repo1/releases/4/assets"), release.UploadURL) case 5: assert.False(t, release.IsDraft) assert.True(t, release.IsPrerelease) + assert.True(t, strings.HasSuffix(release.UploadURL, "/api/v1/repos/user2/repo1/releases/5/assets"), release.UploadURL) default: assert.NoError(t, fmt.Errorf("unexpected release: %v", release)) } @@ -248,3 +255,36 @@ func TestAPIDeleteReleaseByTagName(t *testing.T) { req = NewRequestf(t, http.MethodDelete, fmt.Sprintf("/api/v1/repos/%s/%s/tags/release-tag?token=%s", owner.Name, repo.Name, token)) _ = MakeRequest(t, req, http.StatusNoContent) } + +func TestAPIUploadAssetRelease(t *testing.T) { + defer tests.PrepareTestEnv(t)() + + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) + owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}) + session := loginUser(t, owner.LowerName) + token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository) + + r := createNewReleaseUsingAPI(t, session, token, owner, repo, "release-tag", "", "Release Tag", "test") + + filename := "image.png" + buff := generateImg() + body := &bytes.Buffer{} + + writer := multipart.NewWriter(body) + part, err := writer.CreateFormFile("attachment", filename) + assert.NoError(t, err) + _, err = io.Copy(part, &buff) + assert.NoError(t, err) + err = writer.Close() + assert.NoError(t, err) + + req := NewRequestWithBody(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/releases/%d/assets?name=test-asset&token=%s", owner.Name, repo.Name, r.ID, token), body) + req.Header.Add("Content-Type", writer.FormDataContentType()) + resp := MakeRequest(t, req, http.StatusCreated) + + var attachment *api.Attachment + DecodeJSON(t, resp, &attachment) + + assert.EqualValues(t, "test-asset", attachment.Name) + assert.EqualValues(t, 104, attachment.Size) +} |