Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-21 17:34:24 +01:00

Compare commits

...

22 commits

Author SHA1 Message Date
Nirmal Kumar R ea0003b08d Use await for editor toBeVisible() 2024-11-20 20:55:19 +05:30
Nirmal Kumar R 0c56ae07ad Use expect for editor in e2e + Title case comment 2024-11-20 20:52:10 +05:30
Nirmal Kumar R 27c2a67391 e2e: Use the existing logged_in context for page 2024-11-20 18:27:05 +05:30
Nirmal Kumar R f507aa0be0 Fix for frontend linter issues 2024-11-20 18:27:05 +05:30
Nirmal Kumar R 0228b4f138 Fix the e2e test for Monaco editor 2024-11-20 18:27:05 +05:30
Nirmal Kumar R f70e15966d Fix unnecessary use of networkidle 2024-11-20 18:27:05 +05:30
Nirmal Kumar R e550c4bdca Move the e2e test to markdown-editor.test.e2e.ts 2024-11-20 18:27:05 +05:30
Nirmal Kumar R 76a5f7ba62 Fix frontend-linter issues 2024-11-20 18:27:05 +05:30
Nirmal Kumar R d1b139a9b9 Add e2e test for Markdown image preview 2024-11-20 18:27:05 +05:30
Nirmal Kumar R 803eb8c246 Remove unnecessary URLPrefix assignment 2024-11-20 18:27:05 +05:30
Nirmal Kumar R 3086165b7f Fix data-context + Remove RelativePath attributes 2024-11-20 18:27:05 +05:30
Nirmal Kumar R 1c3faf8adb Revert the change on Wiki ResolveMediaLink 2024-11-20 18:27:05 +05:30
Nirmal Kumar R e665b12564 Fix Wiki media link to call RawLink func 2024-11-20 18:27:05 +05:30
Nirmal Kumar R e813efdf37 Generate swagger for branchPath and relativePath 2024-11-20 18:27:05 +05:30
Nirmal Kumar R c522f67968 Linter fix: Add missing semicolon 2024-11-20 18:27:05 +05:30
Nirmal Kumar R 27d05b69b5 Linter fix: Rename UrlPrefix => URLPrefix 2024-11-20 18:27:05 +05:30
Nirmal Kumar R 4e700f5727 DRY: Add existing ctx value to data-branch-path 2024-11-20 18:27:05 +05:30
Nirmal Kumar R 1527ece6cc Extend API MarkupOptions to contain branch and relative path
The api.MarkupOptions{} struct will have a RelativePath which depicts
the relative path to the repository root + BranchPath which contains the
current branch. The RenderMarkup function utilizes a struct since there
are too many variables passed as arguments and that is not a good sign
for readability.
2024-11-20 18:27:05 +05:30
Nirmal Kumar R 59bc82f1da fix: Preview picture not visible on Markdown file
Fixes the preview picture not showing while editing and previewing the
image on Markdown files. The fix is to the user `/raw/` file path
instead `/src/` path.
2024-11-20 18:27:05 +05:30
JakobDev 45fa9e5ae9 fix: Allow Organisations to remove the Email Address (#5517)
It is possible to set a Email for a Organization. This Email is optional and only used to be displayed on the profile page. However, once you set an EMail, you can no longer remove it. This PR fixes that.

While working on the tests, I found out, that the API returns a 500 when trying to set an invalid EMail. I fixed that too. It returns a 422 now.

Fixes #4567

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/5517
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Reviewed-by: Otto <otto@codeberg.org>
Co-authored-by: JakobDev <jakobdev@gmx.de>
Co-committed-by: JakobDev <jakobdev@gmx.de>
2024-11-20 12:31:34 +00:00
0ko 1316f4d338 Merge pull request 'Fix regression from #4753' (#6029) from JakobDev/forgejo:notefix into forgejo
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6029
Reviewed-by: 0ko <0ko@noreply.codeberg.org>
2024-11-19 19:23:56 +00:00
JakobDev f4c70a3c43
Fix regression from #4753 2024-11-19 19:17:46 +01:00
18 changed files with 349 additions and 31 deletions

View file

@ -139,6 +139,38 @@ func GetPrimaryEmailAddressOfUser(ctx context.Context, uid int64) (*EmailAddress
return ea, nil return ea, nil
} }
// Deletes the primary email address of the user
// This is only allowed if the user is a organization
func DeletePrimaryEmailAddressOfUser(ctx context.Context, uid int64) error {
user, err := GetUserByID(ctx, uid)
if err != nil {
return err
}
if user.Type != UserTypeOrganization {
return fmt.Errorf("%s is not a organization", user.Name)
}
ctx, committer, err := db.TxContext(ctx)
if err != nil {
return err
}
defer committer.Close()
_, err = db.GetEngine(ctx).Exec("DELETE FROM email_address WHERE uid = ? AND is_primary = true", uid)
if err != nil {
return err
}
user.Email = ""
err = UpdateUserCols(ctx, user, "email")
if err != nil {
return err
}
return committer.Commit()
}
// GetEmailAddresses returns all email addresses belongs to given user. // GetEmailAddresses returns all email addresses belongs to given user.
func GetEmailAddresses(ctx context.Context, uid int64) ([]*EmailAddress, error) { func GetEmailAddresses(ctx context.Context, uid int64) ([]*EmailAddress, error) {
emails := make([]*EmailAddress, 0, 5) emails := make([]*EmailAddress, 0, 5)

View file

@ -163,3 +163,21 @@ func TestGetActivatedEmailAddresses(t *testing.T) {
}) })
} }
} }
func TestDeletePrimaryEmailAddressOfUser(t *testing.T) {
require.NoError(t, unittest.PrepareTestDatabase())
user, err := user_model.GetUserByName(db.DefaultContext, "org3")
require.NoError(t, err)
assert.Equal(t, "org3@example.com", user.Email)
require.NoError(t, user_model.DeletePrimaryEmailAddressOfUser(db.DefaultContext, user.ID))
user, err = user_model.GetUserByName(db.DefaultContext, "org3")
require.NoError(t, err)
assert.Empty(t, user.Email)
email, err := user_model.GetPrimaryEmailAddressOfUser(db.DefaultContext, user.ID)
assert.True(t, user_model.IsErrEmailAddressNotExist(err))
assert.Nil(t, email)
}

View file

@ -37,6 +37,10 @@ type MarkupOption struct {
// //
// in: body // in: body
FilePath string FilePath string
// The current branch path where the form gets posted
//
// in: body
BranchPath string
} }
// MarkupRender is a rendered markup document // MarkupRender is a rendered markup document

View file

@ -47,11 +47,11 @@ type CreateOrgOption struct {
// EditOrgOption options for editing an organization // EditOrgOption options for editing an organization
type EditOrgOption struct { type EditOrgOption struct {
FullName string `json:"full_name" binding:"MaxSize(100)"` FullName string `json:"full_name" binding:"MaxSize(100)"`
Email string `json:"email" binding:"MaxSize(255)"` Email *string `json:"email" binding:"MaxSize(255)"`
Description string `json:"description" binding:"MaxSize(255)"` Description string `json:"description" binding:"MaxSize(255)"`
Website string `json:"website" binding:"ValidUrl;MaxSize(255)"` Website string `json:"website" binding:"ValidUrl;MaxSize(255)"`
Location string `json:"location" binding:"MaxSize(50)"` Location string `json:"location" binding:"MaxSize(50)"`
// possible values are `public`, `limited` or `private` // possible values are `public`, `limited` or `private`
// enum: ["public", "limited", "private"] // enum: ["public", "limited", "private"]
Visibility string `json:"visibility" binding:"In(,public,limited,private)"` Visibility string `json:"visibility" binding:"In(,public,limited,private)"`

View file

@ -41,7 +41,16 @@ func Markup(ctx *context.APIContext) {
return return
} }
common.RenderMarkup(ctx.Base, ctx.Repo, form.Mode, form.Text, form.Context, form.FilePath, form.Wiki) re := common.Renderer{
Mode: form.Mode,
Text: form.Text,
URLPrefix: form.Context,
FilePath: form.FilePath,
BranchPath: form.BranchPath,
IsWiki: form.Wiki,
}
re.RenderMarkup(ctx.Base, ctx.Repo)
} }
// Markdown render markdown document to HTML // Markdown render markdown document to HTML
@ -76,7 +85,14 @@ func Markdown(ctx *context.APIContext) {
mode = form.Mode mode = form.Mode
} }
common.RenderMarkup(ctx.Base, ctx.Repo, mode, form.Text, form.Context, "", form.Wiki) re := common.Renderer{
Mode: mode,
Text: form.Text,
URLPrefix: form.Context,
IsWiki: form.Wiki,
}
re.RenderMarkup(ctx.Base, ctx.Repo)
} }
// MarkdownRaw render raw markdown HTML // MarkdownRaw render raw markdown HTML

View file

@ -15,6 +15,7 @@ import (
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/optional" "code.gitea.io/gitea/modules/optional"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/validation"
"code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/routers/api/v1/user" "code.gitea.io/gitea/routers/api/v1/user"
"code.gitea.io/gitea/routers/api/v1/utils" "code.gitea.io/gitea/routers/api/v1/utils"
@ -340,13 +341,28 @@ func Edit(ctx *context.APIContext) {
// "$ref": "#/responses/Organization" // "$ref": "#/responses/Organization"
// "404": // "404":
// "$ref": "#/responses/notFound" // "$ref": "#/responses/notFound"
// "422":
// "$ref": "#/responses/error"
form := web.GetForm(ctx).(*api.EditOrgOption) form := web.GetForm(ctx).(*api.EditOrgOption)
if form.Email != "" { if form.Email != nil {
if err := user_service.ReplacePrimaryEmailAddress(ctx, ctx.Org.Organization.AsUser(), form.Email); err != nil { if *form.Email == "" {
ctx.Error(http.StatusInternalServerError, "ReplacePrimaryEmailAddress", err) err := user_model.DeletePrimaryEmailAddressOfUser(ctx, ctx.Org.Organization.ID)
return if err != nil {
ctx.Error(http.StatusInternalServerError, "DeletePrimaryEmailAddressOfUser", err)
return
}
ctx.Org.Organization.Email = ""
} else {
if err := user_service.ReplacePrimaryEmailAddress(ctx, ctx.Org.Organization.AsUser(), *form.Email); err != nil {
if validation.IsErrEmailInvalid(err) || validation.IsErrEmailCharIsNotSupported(err) {
ctx.Error(http.StatusUnprocessableEntity, "ReplacePrimaryEmailAddress", err)
} else {
ctx.Error(http.StatusInternalServerError, "ReplacePrimaryEmailAddress", err)
}
return
}
} }
} }

View file

@ -18,26 +18,31 @@ import (
"mvdan.cc/xurls/v2" "mvdan.cc/xurls/v2"
) )
type Renderer struct {
Mode, Text, URLPrefix, FilePath, BranchPath string
IsWiki bool
}
// RenderMarkup renders markup text for the /markup and /markdown endpoints // RenderMarkup renders markup text for the /markup and /markdown endpoints
func RenderMarkup(ctx *context.Base, repo *context.Repository, mode, text, urlPrefix, filePath string, wiki bool) { func (re *Renderer) RenderMarkup(ctx *context.Base, repo *context.Repository) {
var markupType string var markupType string
relativePath := "" relativePath := ""
if len(text) == 0 { if len(re.Text) == 0 {
_, _ = ctx.Write([]byte("")) _, _ = ctx.Write([]byte(""))
return return
} }
switch mode { switch re.Mode {
case "markdown": case "markdown":
// Raw markdown // Raw markdown
if err := markdown.RenderRaw(&markup.RenderContext{ if err := markdown.RenderRaw(&markup.RenderContext{
Ctx: ctx, Ctx: ctx,
Links: markup.Links{ Links: markup.Links{
AbsolutePrefix: true, AbsolutePrefix: true,
Base: urlPrefix, Base: re.URLPrefix,
}, },
}, strings.NewReader(text), ctx.Resp); err != nil { }, strings.NewReader(re.Text), ctx.Resp); err != nil {
ctx.Error(http.StatusInternalServerError, err.Error()) ctx.Error(http.StatusInternalServerError, err.Error())
} }
return return
@ -50,30 +55,30 @@ func RenderMarkup(ctx *context.Base, repo *context.Repository, mode, text, urlPr
case "file": case "file":
// File as document based on file extension // File as document based on file extension
markupType = "" markupType = ""
relativePath = filePath relativePath = re.FilePath
default: default:
ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("Unknown mode: %s", mode)) ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("Unknown mode: %s", re.Mode))
return return
} }
if !strings.HasPrefix(setting.AppSubURL+"/", urlPrefix) { if !strings.HasPrefix(setting.AppSubURL+"/", re.URLPrefix) {
// check if urlPrefix is already set to a URL // check if urlPrefix is already set to a URL
linkRegex, _ := xurls.StrictMatchingScheme("https?://") linkRegex, _ := xurls.StrictMatchingScheme("https?://")
m := linkRegex.FindStringIndex(urlPrefix) m := linkRegex.FindStringIndex(re.URLPrefix)
if m == nil { if m == nil {
urlPrefix = util.URLJoin(setting.AppURL, urlPrefix) re.URLPrefix = util.URLJoin(setting.AppURL, re.URLPrefix)
} }
} }
meta := map[string]string{} meta := map[string]string{}
if repo != nil && repo.Repository != nil { if repo != nil && repo.Repository != nil {
if mode == "comment" { if re.Mode == "comment" {
meta = repo.Repository.ComposeMetas(ctx) meta = repo.Repository.ComposeMetas(ctx)
} else { } else {
meta = repo.Repository.ComposeDocumentMetas(ctx) meta = repo.Repository.ComposeDocumentMetas(ctx)
} }
} }
if mode != "comment" { if re.Mode != "comment" {
meta["mode"] = "document" meta["mode"] = "document"
} }
@ -81,13 +86,14 @@ func RenderMarkup(ctx *context.Base, repo *context.Repository, mode, text, urlPr
Ctx: ctx, Ctx: ctx,
Links: markup.Links{ Links: markup.Links{
AbsolutePrefix: true, AbsolutePrefix: true,
Base: urlPrefix, Base: re.URLPrefix,
BranchPath: re.BranchPath,
}, },
Metas: meta, Metas: meta,
IsWiki: wiki, IsWiki: re.IsWiki,
Type: markupType, Type: markupType,
RelativePath: relativePath, RelativePath: relativePath,
}, strings.NewReader(text), ctx.Resp); err != nil { }, strings.NewReader(re.Text), ctx.Resp); err != nil {
if markup.IsErrUnsupportedRenderExtension(err) { if markup.IsErrUnsupportedRenderExtension(err) {
ctx.Error(http.StatusUnprocessableEntity, err.Error()) ctx.Error(http.StatusUnprocessableEntity, err.Error())
} else { } else {

View file

@ -14,5 +14,15 @@ import (
// Markup render markup document to HTML // Markup render markup document to HTML
func Markup(ctx *context.Context) { func Markup(ctx *context.Context) {
form := web.GetForm(ctx).(*api.MarkupOption) form := web.GetForm(ctx).(*api.MarkupOption)
common.RenderMarkup(ctx.Base, ctx.Repo, form.Mode, form.Text, form.Context, form.FilePath, form.Wiki)
re := common.Renderer{
Mode: form.Mode,
Text: form.Text,
URLPrefix: form.Context,
FilePath: form.FilePath,
BranchPath: form.BranchPath,
IsWiki: form.Wiki,
}
re.RenderMarkup(ctx.Base, ctx.Repo)
} }

View file

@ -93,7 +93,13 @@ func SettingsPost(ctx *context.Context) {
ctx.Org.OrgLink = setting.AppSubURL + "/org/" + url.PathEscape(org.Name) ctx.Org.OrgLink = setting.AppSubURL + "/org/" + url.PathEscape(org.Name)
} }
if form.Email != "" { if form.Email == "" {
err := user_model.DeletePrimaryEmailAddressOfUser(ctx, org.ID)
if err != nil {
ctx.ServerError("DeletePrimaryEmailAddressOfUser", err)
return
}
} else {
if err := user_service.ReplacePrimaryEmailAddress(ctx, org.AsUser(), form.Email); err != nil { if err := user_service.ReplacePrimaryEmailAddress(ctx, org.AsUser(), form.Email); err != nil {
ctx.Data["Err_Email"] = true ctx.Data["Err_Email"] = true
ctx.RenderWithErr(ctx.Tr("form.email_invalid"), tplSettingsOptions, &form) ctx.RenderWithErr(ctx.Tr("form.email_invalid"), tplSettingsOptions, &form)

View file

@ -211,6 +211,7 @@ func editFile(ctx *context.Context, isNewFile bool) {
ctx.Data["TreeNames"] = treeNames ctx.Data["TreeNames"] = treeNames
ctx.Data["TreePaths"] = treePaths ctx.Data["TreePaths"] = treePaths
ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL() ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()
ctx.Data["BranchPath"] = ctx.Repo.BranchNameSubURL()
ctx.Data["commit_summary"] = "" ctx.Data["commit_summary"] = ""
ctx.Data["commit_message"] = "" ctx.Data["commit_message"] = ""
if canCommit { if canCommit {

View file

@ -278,9 +278,9 @@
<strong>{{.NoteCommit.Author.Name}}</strong> <strong>{{.NoteCommit.Author.Name}}</strong>
{{end}} {{end}}
<span class="text grey" id="note-authored-time">{{DateUtils.TimeSince .NoteCommit.Author.When}}</span> <span class="text grey" id="note-authored-time">{{DateUtils.TimeSince .NoteCommit.Author.When}}</span>
{{if or ($.Permission.CanWrite $.UnitTypeCode) (not $.Repository.IsArchived) (not .IsDeleted)}} {{if and ($.Permission.CanWrite $.UnitTypeCode) (not $.Repository.IsArchived) (not .IsDeleted)}}
<div class="ui right"> <div class="ui right">
<button id="commit-notes-edit-button" class="ui tiny primary button" data-modal="#delete-note-modal">{{ctx.Locale.Tr "edit"}}</button> <button id="commit-notes-edit-button" class="ui tiny primary button">{{ctx.Locale.Tr "edit"}}</button>
<button class="ui tiny button red show-modal" data-modal="#delete-note-modal">{{ctx.Locale.Tr "remove"}}</button> <button class="ui tiny button red show-modal" data-modal="#delete-note-modal">{{ctx.Locale.Tr "remove"}}</button>
</div> </div>
<div class="ui small modal" id="delete-note-modal"> <div class="ui small modal" id="delete-note-modal">

View file

@ -28,7 +28,7 @@
<div class="field"> <div class="field">
<div class="ui top attached tabular menu" data-write="write" data-preview="preview" data-diff="diff"> <div class="ui top attached tabular menu" data-write="write" data-preview="preview" data-diff="diff">
<a class="active item" data-tab="write">{{svg "octicon-code"}} {{if .IsNewFile}}{{ctx.Locale.Tr "repo.editor.new_file"}}{{else}}{{ctx.Locale.Tr "repo.editor.edit_file"}}{{end}}</a> <a class="active item" data-tab="write">{{svg "octicon-code"}} {{if .IsNewFile}}{{ctx.Locale.Tr "repo.editor.new_file"}}{{else}}{{ctx.Locale.Tr "repo.editor.edit_file"}}{{end}}</a>
<a class="item" data-tab="preview" data-url="{{.Repository.Link}}/markup" data-context="{{.RepoLink}}/src/{{.BranchNameSubURL}}" data-markup-mode="file">{{svg "octicon-eye"}} {{ctx.Locale.Tr "preview"}}</a> <a class="item" data-tab="preview" data-url="{{.Repository.Link}}/markup" data-context="{{.RepoLink}}" data-branch-path="{{.BranchNameSubURL}}" data-markup-mode="file">{{svg "octicon-eye"}} {{ctx.Locale.Tr "preview"}}</a>
{{if not .IsNewFile}} {{if not .IsNewFile}}
<a class="item" data-tab="diff" hx-params="context,content" hx-vals='{"context":"{{.BranchLink}}"}' hx-include="#edit_area" hx-swap="innerHTML" hx-target=".tab[data-tab='diff']" hx-indicator=".tab[data-tab='diff']" hx-post="{{.RepoLink}}/_preview/{{.BranchName | PathEscapeSegments}}/{{.TreePath | PathEscapeSegments}}">{{svg "octicon-diff"}} {{ctx.Locale.Tr "repo.editor.preview_changes"}}</a> <a class="item" data-tab="diff" hx-params="context,content" hx-vals='{"context":"{{.BranchLink}}"}' hx-include="#edit_area" hx-swap="innerHTML" hx-target=".tab[data-tab='diff']" hx-indicator=".tab[data-tab='diff']" hx-post="{{.RepoLink}}/_preview/{{.BranchName | PathEscapeSegments}}/{{.TreePath | PathEscapeSegments}}">{{svg "octicon-diff"}} {{ctx.Locale.Tr "repo.editor.preview_changes"}}</a>
{{end}} {{end}}

View file

@ -2263,6 +2263,9 @@
}, },
"404": { "404": {
"$ref": "#/responses/notFound" "$ref": "#/responses/notFound"
},
"422": {
"$ref": "#/responses/error"
} }
} }
} }
@ -24319,6 +24322,10 @@
"description": "MarkupOption markup options", "description": "MarkupOption markup options",
"type": "object", "type": "object",
"properties": { "properties": {
"BranchPath": {
"description": "The current branch path where the form gets posted\n\nin: body",
"type": "string"
},
"Context": { "Context": {
"description": "Context to render\n\nin: body", "description": "Context to render\n\nin: body",
"type": "string" "type": "string"

View file

@ -11,6 +11,36 @@ test.beforeAll(async ({browser}, workerInfo) => {
await login_user(browser, workerInfo, 'user2'); await login_user(browser, workerInfo, 'user2');
}); });
test('Markdown image preview behaviour', async ({browser}, workerInfo) => {
const context = await load_logged_in_context(browser, workerInfo, 'user2');
// Editing the root README.md file for image preview
const editPath = '/user2/repo1/src/branch/master/README.md';
const page = await context.newPage();
const response = await page.goto(editPath, {waitUntil: 'domcontentloaded'});
expect(response?.status()).toBe(200);
// Click 'Edit file' tab
await page.locator('[data-tooltip-content="Edit file"]').click();
// This yields the monaco editor
const editor = page.getByRole('presentation').nth(0);
await expect(editor).toBeVisible();
await editor.click();
// Clear all the content
await page.keyboard.press('ControlOrMeta+KeyA');
// Add the image
await page.keyboard.type('![Logo of Forgejo](./assets/logo.svg "Logo of Forgejo")');
// Click 'Preview' tab
await page.locator('a[data-tab="preview"]').click();
// Check for the image preview via the expected attribute
const preview = page.locator('div[data-tab="preview"] p[dir="auto"] a');
await expect(preview).toHaveAttribute('href', 'http://localhost:3003/user2/repo1/media/branch/master/assets/logo.svg');
});
test('markdown indentation', async ({browser}, workerInfo) => { test('markdown indentation', async ({browser}, workerInfo) => {
const context = await load_logged_in_context(browser, workerInfo, 'user2'); const context = await load_logged_in_context(browser, workerInfo, 'user2');

View file

@ -218,3 +218,57 @@ func TestAPIOrgSearchEmptyTeam(t *testing.T) {
assert.EqualValues(t, "Empty", data.Data[0].Name) assert.EqualValues(t, "Empty", data.Data[0].Name)
} }
} }
func TestAPIOrgChangeEmail(t *testing.T) {
defer tests.PrepareTestEnv(t)()
session := loginUser(t, "user1")
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteOrganization)
t.Run("Invalid", func(t *testing.T) {
newMail := "invalid"
settings := api.EditOrgOption{Email: &newMail}
resp := MakeRequest(t, NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &settings).AddTokenAuth(token), http.StatusUnprocessableEntity)
var org *api.Organization
DecodeJSON(t, resp, &org)
assert.Empty(t, org.Email)
})
t.Run("Valid", func(t *testing.T) {
newMail := "example@example.com"
settings := api.EditOrgOption{Email: &newMail}
resp := MakeRequest(t, NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &settings).AddTokenAuth(token), http.StatusOK)
var org *api.Organization
DecodeJSON(t, resp, &org)
assert.Equal(t, "example@example.com", org.Email)
})
t.Run("NoChange", func(t *testing.T) {
settings := api.EditOrgOption{}
resp := MakeRequest(t, NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &settings).AddTokenAuth(token), http.StatusOK)
var org *api.Organization
DecodeJSON(t, resp, &org)
assert.Equal(t, "example@example.com", org.Email)
})
t.Run("Empty", func(t *testing.T) {
newMail := ""
settings := api.EditOrgOption{Email: &newMail}
resp := MakeRequest(t, NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &settings).AddTokenAuth(token), http.StatusOK)
var org *api.Organization
DecodeJSON(t, resp, &org)
assert.Empty(t, org.Email)
})
}

View file

@ -0,0 +1,89 @@
// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package integration
import (
"fmt"
"net/http"
"testing"
auth_model "code.gitea.io/gitea/models/auth"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/tests"
"github.com/stretchr/testify/assert"
)
func getOrgSettingsFormData(t *testing.T, session *TestSession, orgName string) map[string]string {
return map[string]string{
"_csrf": GetCSRF(t, session, fmt.Sprintf("/org/%s/settings", orgName)),
"name": orgName,
"full_name": "",
"email": "",
"description": "",
"website": "",
"location": "",
"visibility": "0",
"repo_admin_change_team_access": "on",
"max_repo_creation": "-1",
}
}
func getOrgSettings(t *testing.T, token, orgName string) *api.Organization {
t.Helper()
req := NewRequestf(t, "GET", "/api/v1/orgs/%s", orgName).AddTokenAuth(token)
resp := MakeRequest(t, req, http.StatusOK)
var org *api.Organization
DecodeJSON(t, resp, &org)
return org
}
func TestOrgSettingsChangeEmail(t *testing.T) {
defer tests.PrepareTestEnv(t)()
const orgName = "org3"
settingsURL := fmt.Sprintf("/org/%s/settings", orgName)
session := loginUser(t, "user1")
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadOrganization)
t.Run("Invalid", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
settings := getOrgSettingsFormData(t, session, orgName)
settings["email"] = "invalid"
session.MakeRequest(t, NewRequestWithValues(t, "POST", settingsURL, settings), http.StatusOK)
org := getOrgSettings(t, token, orgName)
assert.Equal(t, "org3@example.com", org.Email)
})
t.Run("Valid", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
settings := getOrgSettingsFormData(t, session, orgName)
settings["email"] = "example@example.com"
session.MakeRequest(t, NewRequestWithValues(t, "POST", settingsURL, settings), http.StatusSeeOther)
org := getOrgSettings(t, token, orgName)
assert.Equal(t, "example@example.com", org.Email)
})
t.Run("Empty", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
settings := getOrgSettingsFormData(t, session, orgName)
settings["email"] = ""
session.MakeRequest(t, NewRequestWithValues(t, "POST", settingsURL, settings), http.StatusSeeOther)
org := getOrgSettings(t, token, orgName)
assert.Empty(t, org.Email)
})
}

View file

@ -5,6 +5,8 @@ import (
"net/url" "net/url"
"testing" "testing"
"code.gitea.io/gitea/tests"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -42,3 +44,29 @@ func TestRepoModifyGitNotes(t *testing.T) {
}) })
}) })
} }
func TestRepoGitNotesButtonsVisible(t *testing.T) {
onGiteaRun(t, func(*testing.T, *url.URL) {
t.Run("With Permission", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
session := loginUser(t, "user2")
req := NewRequest(t, "GET", "/user2/repo1/commit/65f1bf27bc3bf70f64657658635e66094edbcb4d")
resp := session.MakeRequest(t, req, http.StatusOK)
assert.Contains(t, resp.Body.String(), "id=\"commit-notes-edit-button\"")
assert.Contains(t, resp.Body.String(), "data-modal=\"#delete-note-modal\"")
})
t.Run("Without Permission", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req := NewRequest(t, "GET", "/user2/repo1/commit/65f1bf27bc3bf70f64657658635e66094edbcb4d")
resp := MakeRequest(t, req, http.StatusOK)
assert.NotContains(t, resp.Body.String(), "id=\"commit-notes-edit-button\"")
assert.NotContains(t, resp.Body.String(), "data-modal=\"#delete-note-modal\"")
})
})
}

View file

@ -26,6 +26,7 @@ function initEditPreviewTab($form) {
const formData = new FormData(); const formData = new FormData();
formData.append('mode', mode); formData.append('mode', mode);
formData.append('context', context); formData.append('context', context);
formData.append('branch_path', $this.data('branch-path'));
formData.append( formData.append(
'text', 'text',
$form.find(`.tab[data-tab="${$tabMenu.data('write')}"] textarea`).val(), $form.find(`.tab[data-tab="${$tabMenu.data('write')}"] textarea`).val(),