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

formatted_data_test.go « wiki « service « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3233c416ae78678a3f9bda8180b1086ebd1c6a6f (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package wiki

import (
	"bytes"
	"io"
	"testing"

	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/gitaly/internal/testhelper"
	"gitlab.com/gitlab-org/gitaly/proto/go/gitalypb"
	"google.golang.org/grpc/codes"
)

func TestSuccessfulWikiGetFormattedDataRequest(t *testing.T) {
	wikiRepo, _, cleanupFunc := setupWikiRepo(t)
	defer cleanupFunc()

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

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

	format := "rdoc"
	content := bytes.Repeat([]byte("*bold*\n\n"), 10000)
	expectedContent := bytes.Repeat([]byte("\n<p><strong>bold</strong></p>\n"), 10000)
	page1Name := "Home Pagé"
	page2Name := "Instálling/Step 133-b"
	page3Name := "Installing/Step 133-c"
	page4Name := "Empty file"
	page1Commit := createTestWikiPage(t, client, wikiRepo, createWikiPageOpts{title: page1Name, format: format, content: content})
	createTestWikiPage(t, client, wikiRepo, createWikiPageOpts{title: page2Name, format: format, content: content})
	createTestWikiPage(t, client, wikiRepo, createWikiPageOpts{title: page3Name, format: format, content: content})
	createTestWikiPage(t, client, wikiRepo, createWikiPageOpts{title: page4Name, format: format, forceContentEmpty: true})

	testCases := []struct {
		desc            string
		request         *gitalypb.WikiGetFormattedDataRequest
		expectedContent []byte
	}{
		{
			desc: "title only",
			request: &gitalypb.WikiGetFormattedDataRequest{
				Repository: wikiRepo,
				Title:      []byte(page1Name),
			},
			expectedContent: expectedContent,
		},
		{
			desc: "title + revision that includes the page",
			request: &gitalypb.WikiGetFormattedDataRequest{
				Repository: wikiRepo,
				Title:      []byte(page1Name),
				Revision:   []byte(page1Commit.Id),
			},
			expectedContent: expectedContent,
		},
		{
			desc: "title + directory that includes the page",
			request: &gitalypb.WikiGetFormattedDataRequest{
				Repository: wikiRepo,
				Title:      []byte("Step 133-b"),
				Directory:  []byte("Instálling"),
			},
			expectedContent: expectedContent,
		},
		{
			desc: "title for file with empty content",
			request: &gitalypb.WikiGetFormattedDataRequest{
				Repository: wikiRepo,
				Title:      []byte("Empty file"),
			},
			expectedContent: nil,
		},
	}

	for _, testCase := range testCases {
		t.Run(testCase.desc, func(t *testing.T) {
			ctx, cancel := testhelper.Context()
			defer cancel()

			c, err := client.WikiGetFormattedData(ctx, testCase.request)
			require.NoError(t, err)

			receivedData := readFullDataFromWikiGetFormattedDataClient(t, c)
			require.Equal(t, testCase.expectedContent, receivedData)
		})
	}
}

func TestFailedWikiGetFormattedDataDueToValidation(t *testing.T) {
	wikiRepo, _, cleanupFunc := setupWikiRepo(t)
	defer cleanupFunc()

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

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

	testCases := []struct {
		desc  string
		title string
		code  codes.Code
	}{
		{
			desc:  "empty page path",
			title: "",
			code:  codes.InvalidArgument,
		},
		{
			desc:  "non-existent page",
			title: "i-do-not-exist",
			code:  codes.NotFound,
		},
	}

	for _, testCase := range testCases {
		t.Run(testCase.desc, func(t *testing.T) {
			ctx, cancel := testhelper.Context()
			defer cancel()

			request := &gitalypb.WikiGetFormattedDataRequest{
				Repository: wikiRepo,
				Title:      []byte(testCase.title),
			}

			c, err := client.WikiGetFormattedData(ctx, request)
			require.NoError(t, err)

			err = drainWikiGetFormattedDataResponse(c)
			testhelper.RequireGrpcError(t, err, testCase.code)
		})
	}
}

func drainWikiGetFormattedDataResponse(c gitalypb.WikiService_WikiGetFormattedDataClient) error {
	for {
		_, err := c.Recv()
		if err != nil {
			return err
		}
	}
}

func readFullDataFromWikiGetFormattedDataClient(t *testing.T, c gitalypb.WikiService_WikiGetFormattedDataClient) (data []byte) {
	for {
		resp, err := c.Recv()
		if err == io.EOF {
			break
		} else if err != nil {
			t.Fatal(err)
		}

		data = append(data, resp.GetData()...)
	}

	return
}