Welcome to mirror list, hosted at ThFree Co, Russian Federation.

refexists_test.go « ref « service « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 44fa71d93c8f387adc362ec1d1a72da7af86b3f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package ref

import (
	"testing"

	"gitlab.com/gitlab-org/gitaly-proto/go/gitalypb"
	"gitlab.com/gitlab-org/gitaly/internal/helper"
	"gitlab.com/gitlab-org/gitaly/internal/testhelper"
	"google.golang.org/grpc/codes"
)

func TestRefExists(t *testing.T) {
	testRepo, _, cleanupFn := testhelper.NewTestRepo(t)
	defer cleanupFn()

	badRepo := &gitalypb.Repository{StorageName: "invalid", RelativePath: "/etc/"}

	tests := []struct {
		name    string
		ref     string
		want    bool
		repo    *gitalypb.Repository
		wantErr codes.Code
	}{
		{"master", "refs/heads/master", true, testRepo, codes.OK},
		{"v1.0.0", "refs/tags/v1.0.0", true, testRepo, codes.OK},
		{"quoted", "refs/heads/'test'", true, testRepo, codes.OK},
		{"unicode exists", "refs/heads/ʕ•ᴥ•ʔ", true, testRepo, codes.OK},
		{"unicode missing", "refs/tags/अस्तित्वहीन", false, testRepo, codes.OK},
		{"spaces", "refs/ /heads", false, testRepo, codes.OK},
		{"haxxors", "refs/; rm -rf /tmp/*", false, testRepo, codes.OK},
		{"dashes", "--", false, testRepo, codes.InvalidArgument},
		{"blank", "", false, testRepo, codes.InvalidArgument},
		{"not tags or branches", "refs/heads/remotes/origin", false, testRepo, codes.OK},
		{"wildcards", "refs/heads/*", false, testRepo, codes.OK},
		{"invalid repos", "refs/heads/master", false, badRepo, codes.InvalidArgument},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			ctx, cancel := testhelper.Context()
			defer cancel()

			server, serverSocketPath := runRefServiceServer(t)
			defer server.Stop()

			client, conn := newRefServiceClient(t, serverSocketPath)
			defer conn.Close()

			req := &gitalypb.RefExistsRequest{Repository: tt.repo, Ref: []byte(tt.ref)}

			got, err := client.RefExists(ctx, req)

			if helper.GrpcCode(err) != tt.wantErr {
				t.Errorf("server.RefExists() error = %v, wantErr %v", err, tt.wantErr)
				return
			}

			if tt.wantErr != codes.OK {
				if got != nil {
					t.Errorf("server.RefExists() = %v, want null", got)
				}
				return
			}

			if got.Value != tt.want {
				t.Errorf("server.RefExists() = %v, want %v", got.Value, tt.want)
			}
		})
	}
}