Skip to content

Commit

Permalink
Add delete test + fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sapk committed Nov 24, 2017
1 parent 18eed10 commit 61606fc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
19 changes: 17 additions & 2 deletions integrations/api_repo_lfs_locks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package integrations

import (
"fmt"
"io/ioutil"
"net/http"
"strings"
Expand Down Expand Up @@ -97,7 +96,6 @@ func TestAPILFSLocksLogged(t *testing.T) {
req.Header.Set("Content-Type", "application/vnd.git-lfs+json")
req.Body = ioutil.NopCloser(strings.NewReader("{}"))
resp = session.MakeRequest(t, req, http.StatusOK)
fmt.Println(string(resp.Body))
var lfsLocksVerify api.LFSLockListVerify
DecodeJSON(t, resp, &lfsLocksVerify)
assert.Len(t, lfsLocksVerify.Ours, 2)
Expand All @@ -106,4 +104,21 @@ func TestAPILFSLocksLogged(t *testing.T) {
assert.EqualValues(t, user.DisplayName(), lock.Owner.Name)
}

req = NewRequestf(t, "POST", "/%s/%s/info/lfs/locks/%s/unlock", user.Name, repo.Name, lfsLocksVerify.Ours[0].ID)
req.Header.Set("Accept", "application/vnd.git-lfs+json")
req.Header.Set("Content-Type", "application/vnd.git-lfs+json")
req.Body = ioutil.NopCloser(strings.NewReader("{}"))
resp = session.MakeRequest(t, req, http.StatusOK)
var lfsLockRep api.LFSLockResponse
DecodeJSON(t, resp, &lfsLockRep)
assert.Equal(t, lfsLocksVerify.Ours[0].ID, lfsLockRep.Lock.ID)
assert.Equal(t, user.DisplayName(), lfsLockRep.Lock.Owner.Name)

req = NewRequestf(t, "GET", "/%s/%s/info/lfs/locks", user.Name, repo.Name)
req.Header.Set("Accept", "application/vnd.git-lfs+json")
req.Header.Set("Content-Type", "application/vnd.git-lfs+json")
resp = session.MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &lfsLocks)
assert.Len(t, lfsLocks.Locks, 1)

}
11 changes: 5 additions & 6 deletions models/lfs_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,24 +130,23 @@ func GetLFSLockByRepoID(repoID int64) (locks []*LFSLock, err error) {
}

// DeleteLFSLockByID deletes a lock by given ID.
func DeleteLFSLockByID(id int64, u *User, force bool) error {

func DeleteLFSLockByID(id int64, u *User, force bool) (*LFSLock, error) {
lock, err := GetLFSLockByID(id)
if err != nil {
return err
return nil, err
}

err = CheckLFSAccessForRepo(u, lock.RepoID, "delete")
if err != nil {
return err
return nil, err
}

if !force && u.ID != lock.OwnerID {
return fmt.Errorf("user doesn't own lock and force flag is not set")
return nil, fmt.Errorf("user doesn't own lock and force flag is not set")
}

_, err = x.ID(id).Delete(new(LFSLock))
return err
return lock, err
}

//CheckLFSAccessForRepo check needed access mode base on action
Expand Down
4 changes: 2 additions & 2 deletions modules/lfs/locks.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func UnLockHandler(ctx *context.Context) {
return
}

err = models.DeleteLFSLockByID(ctx.ParamsInt64("id"), ctx.User, req.Force)
lock, err := models.DeleteLFSLockByID(ctx.ParamsInt64("lid"), ctx.User, req.Force)
if err != nil {
if models.IsErrLFSLockUnauthorizedAction(err) {
ctx.JSON(403, api.LFSLockError{
Expand All @@ -228,5 +228,5 @@ func UnLockHandler(ctx *context.Context) {
})
return
}
ctx.JSON(404, api.LFSLockError{Message: "Not found"})
ctx.JSON(200, api.LFSLockResponse{Lock: lock.APIFormat()})
}
2 changes: 1 addition & 1 deletion routers/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Get("/", lfs.GetListLockHandler)
m.Post("/", lfs.PostLockHandler)
m.Post("/verify", lfs.VerifyLockHandler)
m.Post("/:id/unlock", lfs.UnLockHandler)
m.Post("/:lid/unlock", lfs.UnLockHandler)
}, context.RepoAssignment())
m.Any("/*", func(ctx *context.Context) {
ctx.Handle(404, "", nil)
Expand Down

0 comments on commit 61606fc

Please sign in to comment.