Skip to content

Commit 80aea31

Browse files
authored
Merge pull request #258 from miantiao-me/copilot/fix-password-removal-issue
fix: allow removing a password from a link on edit
2 parents 0738b43 + 592d2b8 commit 80aea31

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

server/api/link/edit.put.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ export default eventHandler(async (event) => {
5858
createdAt: existingLink.createdAt,
5959
updatedAt: Math.floor(Date.now() / 1000),
6060
}
61+
if (link.password === undefined) {
62+
delete newLink.password
63+
}
6164
await putLink(event, newLink)
6265
setResponseStatus(event, 201)
6366
const shortLink = buildShortLink(event, newLink.slug)

tests/api/link.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,22 @@ describe.sequential('/api/link/edit', () => {
175175
expect(data).toHaveProperty('shortLink')
176176
})
177177

178+
it('removes password when not provided in edit', async () => {
179+
const slug = testLinkPayload.slug
180+
181+
// Set a password on the link
182+
const setPasswordResponse = await putJson('/api/link/edit', { ...testLinkPayload, password: 'secret123' })
183+
expect(setPasswordResponse.status).toBe(201)
184+
const setData = await setPasswordResponse.json() as { link: { password?: string } }
185+
expect(setData.link.password).toBe('secret123')
186+
187+
// Edit the link without providing a password (user cleared the field)
188+
const removePasswordResponse = await putJson('/api/link/edit', { url: testLinkPayload.url, slug })
189+
expect(removePasswordResponse.status).toBe(201)
190+
const removeData = await removePasswordResponse.json() as { link: { password?: string } }
191+
expect(removeData.link.password).toBeUndefined()
192+
})
193+
178194
it('returns 404 when editing non-existent link', async () => {
179195
const payload = { url: 'https://example.com', slug: 'non-existent-slug-for-edit-12345' }
180196
const response = await putJson('/api/link/edit', payload)

0 commit comments

Comments
 (0)