Browse Source

Unit tests for repo redirects (#961)

Ethan Koenig 2 years ago
parent
commit
140967f002
2 changed files with 79 additions and 0 deletions
  1. 5 0
      models/fixtures/repo_redirect.yml
  2. 74 0
      models/repo_redirect_test.go

+ 5 - 0
models/fixtures/repo_redirect.yml

@@ -0,0 +1,5 @@
1
+-
2
+  id: 1
3
+  owner_id: 2
4
+  lower_name: oldrepo1
5
+  redirect_repo_id: 1

+ 74 - 0
models/repo_redirect_test.go

@@ -0,0 +1,74 @@
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 models
6
+
7
+import (
8
+	"testing"
9
+
10
+	"github.com/stretchr/testify/assert"
11
+)
12
+
13
+func TestLookupRepoRedirect(t *testing.T) {
14
+	assert.NoError(t, PrepareTestDatabase())
15
+
16
+	repoID, err := LookupRepoRedirect(2, "oldrepo1")
17
+	assert.NoError(t, err)
18
+	assert.EqualValues(t, 1, repoID)
19
+
20
+	_, err = LookupRepoRedirect(NonexistentID, "doesnotexist")
21
+	assert.True(t, IsErrRepoRedirectNotExist(err))
22
+}
23
+
24
+func TestNewRepoRedirect(t *testing.T) {
25
+	// redirect to a completely new name
26
+	assert.NoError(t, PrepareTestDatabase())
27
+
28
+	repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
29
+	assert.NoError(t, NewRepoRedirect(repo.OwnerID, repo.ID, repo.Name, "newreponame"))
30
+
31
+	AssertExistsAndLoadBean(t, &RepoRedirect{
32
+		OwnerID:        repo.OwnerID,
33
+		LowerName:      repo.LowerName,
34
+		RedirectRepoID: repo.ID,
35
+	})
36
+	AssertExistsAndLoadBean(t, &RepoRedirect{
37
+		OwnerID:        repo.OwnerID,
38
+		LowerName:      "oldrepo1",
39
+		RedirectRepoID: repo.ID,
40
+	})
41
+}
42
+
43
+func TestNewRepoRedirect2(t *testing.T) {
44
+	// redirect to previously used name
45
+	assert.NoError(t, PrepareTestDatabase())
46
+
47
+	repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
48
+	assert.NoError(t, NewRepoRedirect(repo.OwnerID, repo.ID, repo.Name, "oldrepo1"))
49
+
50
+	AssertExistsAndLoadBean(t, &RepoRedirect{
51
+		OwnerID:        repo.OwnerID,
52
+		LowerName:      repo.LowerName,
53
+		RedirectRepoID: repo.ID,
54
+	})
55
+	AssertNotExistsBean(t, &RepoRedirect{
56
+		OwnerID:        repo.OwnerID,
57
+		LowerName:      "oldrepo1",
58
+		RedirectRepoID: repo.ID,
59
+	})
60
+}
61
+
62
+func TestNewRepoRedirect3(t *testing.T) {
63
+	// redirect for a previously-unredirected repo
64
+	assert.NoError(t, PrepareTestDatabase())
65
+
66
+	repo := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
67
+	assert.NoError(t, NewRepoRedirect(repo.OwnerID, repo.ID, repo.Name, "newreponame"))
68
+
69
+	AssertExistsAndLoadBean(t, &RepoRedirect{
70
+		OwnerID:        repo.OwnerID,
71
+		LowerName:      repo.LowerName,
72
+		RedirectRepoID: repo.ID,
73
+	})
74
+}