|
| 1 | +// Copyright 2017 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package integrations |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "code.gitea.io/gitea/models" |
| 14 | + "code.gitea.io/gitea/modules/setting" |
| 15 | + api "code.gitea.io/sdk/gitea" |
| 16 | + |
| 17 | + "github.com/stretchr/testify/assert" |
| 18 | +) |
| 19 | + |
| 20 | +func TestAPILFSLocksNotStarted(t *testing.T) { |
| 21 | + prepareTestEnv(t) |
| 22 | + setting.LFS.StartServer = false |
| 23 | + user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) |
| 24 | + repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) |
| 25 | + |
| 26 | + req := NewRequestf(t, "GET", "/%s/%s/info/lfs/locks", user.Name, repo.Name) |
| 27 | + MakeRequest(t, req, http.StatusNotFound) |
| 28 | + req = NewRequestf(t, "POST", "/%s/%s/info/lfs/locks", user.Name, repo.Name) |
| 29 | + MakeRequest(t, req, http.StatusNotFound) |
| 30 | + req = NewRequestf(t, "GET", "/%s/%s/info/lfs/locks/verify", user.Name, repo.Name) |
| 31 | + MakeRequest(t, req, http.StatusNotFound) |
| 32 | + req = NewRequestf(t, "GET", "/%s/%s/info/lfs/locks/10/unlock", user.Name, repo.Name) |
| 33 | + MakeRequest(t, req, http.StatusNotFound) |
| 34 | +} |
| 35 | + |
| 36 | +func TestAPILFSLocksNotLogin(t *testing.T) { |
| 37 | + prepareTestEnv(t) |
| 38 | + setting.LFS.StartServer = true |
| 39 | + user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) |
| 40 | + repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) |
| 41 | + |
| 42 | + req := NewRequestf(t, "GET", "/%s/%s/info/lfs/locks", user.Name, repo.Name) |
| 43 | + req.Header.Set("Accept", "application/vnd.git-lfs+json") |
| 44 | + req.Header.Set("Content-Type", "application/vnd.git-lfs+json") |
| 45 | + resp := MakeRequest(t, req, http.StatusForbidden) |
| 46 | + var lfsLockError api.LFSLockError |
| 47 | + DecodeJSON(t, resp, &lfsLockError) |
| 48 | + assert.Equal(t, "You must have pull access to list locks : User undefined doesn't have rigth to list for lfs lock [rid: 1]", lfsLockError.Message) |
| 49 | +} |
| 50 | + |
| 51 | +func TestAPILFSLocksLogged(t *testing.T) { |
| 52 | + prepareTestEnv(t) |
| 53 | + setting.LFS.StartServer = true |
| 54 | + user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) //in org 3 |
| 55 | + user4 := models.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) //in org 3 |
| 56 | + |
| 57 | + repo1 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) |
| 58 | + repo3 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // own by org 3 |
| 59 | + |
| 60 | + tests := []struct { |
| 61 | + user *models.User |
| 62 | + repo *models.Repository |
| 63 | + path string |
| 64 | + httpResult int |
| 65 | + addTime []int |
| 66 | + }{ |
| 67 | + {user: user2, repo: repo1, path: "foo/bar.zip", httpResult: http.StatusCreated, addTime: []int{0}}, |
| 68 | + {user: user2, repo: repo1, path: "path/test", httpResult: http.StatusCreated, addTime: []int{0}}, |
| 69 | + {user: user2, repo: repo1, path: "path/test", httpResult: http.StatusConflict}, |
| 70 | + {user: user2, repo: repo1, path: "Foo/BaR.zip", httpResult: http.StatusConflict}, |
| 71 | + {user: user2, repo: repo1, path: "Foo/Test/../subFOlder/../Relative/../BaR.zip", httpResult: http.StatusConflict}, |
| 72 | + {user: user4, repo: repo1, path: "FoO/BaR.zip", httpResult: http.StatusForbidden}, |
| 73 | + {user: user4, repo: repo1, path: "path/test-user4", httpResult: http.StatusForbidden}, |
| 74 | + {user: user2, repo: repo1, path: "patH/Test-user4", httpResult: http.StatusCreated, addTime: []int{0}}, |
| 75 | + {user: user2, repo: repo1, path: "some/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/long/path", httpResult: http.StatusCreated, addTime: []int{0}}, |
| 76 | + |
| 77 | + {user: user2, repo: repo3, path: "test/foo/bar.zip", httpResult: http.StatusCreated, addTime: []int{1, 2}}, |
| 78 | + {user: user4, repo: repo3, path: "test/foo/bar.zip", httpResult: http.StatusConflict}, |
| 79 | + {user: user4, repo: repo3, path: "test/foo/bar.bin", httpResult: http.StatusCreated, addTime: []int{1, 2}}, |
| 80 | + } |
| 81 | + |
| 82 | + resultsTests := []struct { |
| 83 | + user *models.User |
| 84 | + repo *models.Repository |
| 85 | + totalCount int |
| 86 | + oursCount int |
| 87 | + theirsCount int |
| 88 | + locksOwners []*models.User |
| 89 | + locksTimes []time.Time |
| 90 | + }{ |
| 91 | + {user: user2, repo: repo1, totalCount: 4, oursCount: 4, theirsCount: 0, locksOwners: []*models.User{user2, user2, user2, user2}, locksTimes: []time.Time{}}, |
| 92 | + {user: user2, repo: repo3, totalCount: 2, oursCount: 1, theirsCount: 1, locksOwners: []*models.User{user2, user4}, locksTimes: []time.Time{}}, |
| 93 | + {user: user4, repo: repo3, totalCount: 2, oursCount: 1, theirsCount: 1, locksOwners: []*models.User{user2, user4}, locksTimes: []time.Time{}}, |
| 94 | + } |
| 95 | + |
| 96 | + deleteTests := []struct { |
| 97 | + user *models.User |
| 98 | + repo *models.Repository |
| 99 | + lockID string |
| 100 | + }{} |
| 101 | + |
| 102 | + //create locks |
| 103 | + for _, test := range tests { |
| 104 | + session := loginUser(t, test.user.Name) |
| 105 | + req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/%s/info/lfs/locks", test.repo.FullName()), map[string]string{"path": test.path}) |
| 106 | + req.Header.Set("Accept", "application/vnd.git-lfs+json") |
| 107 | + req.Header.Set("Content-Type", "application/vnd.git-lfs+json") |
| 108 | + session.MakeRequest(t, req, test.httpResult) |
| 109 | + if len(test.addTime) > 0 { |
| 110 | + for _, id := range test.addTime { |
| 111 | + resultsTests[id].locksTimes = append(resultsTests[id].locksTimes, time.Now()) |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + //check creation |
| 117 | + for _, test := range resultsTests { |
| 118 | + session := loginUser(t, test.user.Name) |
| 119 | + req := NewRequestf(t, "GET", "/%s/info/lfs/locks", test.repo.FullName()) |
| 120 | + req.Header.Set("Accept", "application/vnd.git-lfs+json") |
| 121 | + req.Header.Set("Content-Type", "application/vnd.git-lfs+json") |
| 122 | + resp := session.MakeRequest(t, req, http.StatusOK) |
| 123 | + var lfsLocks api.LFSLockList |
| 124 | + DecodeJSON(t, resp, &lfsLocks) |
| 125 | + assert.Len(t, lfsLocks.Locks, test.totalCount) |
| 126 | + for i, lock := range lfsLocks.Locks { |
| 127 | + assert.EqualValues(t, test.locksOwners[i].DisplayName(), lock.Owner.Name) |
| 128 | + assert.WithinDuration(t, test.locksTimes[i], lock.LockedAt, 1*time.Second) |
| 129 | + } |
| 130 | + |
| 131 | + req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/%s/info/lfs/locks/verify", test.repo.FullName()), map[string]string{}) |
| 132 | + req.Header.Set("Accept", "application/vnd.git-lfs+json") |
| 133 | + req.Header.Set("Content-Type", "application/vnd.git-lfs+json") |
| 134 | + resp = session.MakeRequest(t, req, http.StatusOK) |
| 135 | + var lfsLocksVerify api.LFSLockListVerify |
| 136 | + DecodeJSON(t, resp, &lfsLocksVerify) |
| 137 | + assert.Len(t, lfsLocksVerify.Ours, test.oursCount) |
| 138 | + assert.Len(t, lfsLocksVerify.Theirs, test.theirsCount) |
| 139 | + for _, lock := range lfsLocksVerify.Ours { |
| 140 | + assert.EqualValues(t, test.user.DisplayName(), lock.Owner.Name) |
| 141 | + deleteTests = append(deleteTests, struct { |
| 142 | + user *models.User |
| 143 | + repo *models.Repository |
| 144 | + lockID string |
| 145 | + }{test.user, test.repo, lock.ID}) |
| 146 | + } |
| 147 | + for _, lock := range lfsLocksVerify.Theirs { |
| 148 | + assert.NotEqual(t, test.user.DisplayName(), lock.Owner.Name) |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + //remove all locks |
| 153 | + for _, test := range deleteTests { |
| 154 | + session := loginUser(t, test.user.Name) |
| 155 | + req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/%s/info/lfs/locks/%s/unlock", test.repo.FullName(), test.lockID), map[string]string{}) |
| 156 | + req.Header.Set("Accept", "application/vnd.git-lfs+json") |
| 157 | + req.Header.Set("Content-Type", "application/vnd.git-lfs+json") |
| 158 | + resp := session.MakeRequest(t, req, http.StatusOK) |
| 159 | + var lfsLockRep api.LFSLockResponse |
| 160 | + DecodeJSON(t, resp, &lfsLockRep) |
| 161 | + assert.Equal(t, test.lockID, lfsLockRep.Lock.ID) |
| 162 | + assert.Equal(t, test.user.DisplayName(), lfsLockRep.Lock.Owner.Name) |
| 163 | + } |
| 164 | + |
| 165 | + // check that we don't have any lock |
| 166 | + for _, test := range resultsTests { |
| 167 | + session := loginUser(t, test.user.Name) |
| 168 | + req := NewRequestf(t, "GET", "/%s/info/lfs/locks", test.repo.FullName()) |
| 169 | + req.Header.Set("Accept", "application/vnd.git-lfs+json") |
| 170 | + req.Header.Set("Content-Type", "application/vnd.git-lfs+json") |
| 171 | + resp := session.MakeRequest(t, req, http.StatusOK) |
| 172 | + var lfsLocks api.LFSLockList |
| 173 | + DecodeJSON(t, resp, &lfsLocks) |
| 174 | + assert.Len(t, lfsLocks.Locks, 0) |
| 175 | + } |
| 176 | +} |
0 commit comments