Repository navigation
Expand file tree
/
Copy pathplatform_test.go
More file actions
159 lines (137 loc) · 5.4 KB
/
Copy pathplatform_test.go
File metadata and controls
159 lines (137 loc) · 5.4 KB
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 mark
import (
"context"
"io"
"net/http"
"path/filepath"
"strings"
"sync"
"testing"
"github.com/kovetskiy/mark/v17/confluence"
"github.com/kovetskiy/mark/v17/confluence/confluencetest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestRunStopsWhenThePlatformCannotBeIdentified: Cloud and Server take
// different paths for folders, moves and the manifest, so a run that cannot
// tell which one it is talking to stops before it publishes anything.
func TestRunStopsWhenThePlatformCannotBeIdentified(t *testing.T) {
server, _ := docsSpace(t)
server.SetFail(func(r *http.Request) (int, string, bool) {
if r.URL.Path == "/rest/api/user/current" {
return http.StatusForbidden, `{"message":"forbidden"}`, true
}
return 0, "", false
})
dir := t.TempDir()
writeFile(t, dir, "doc.md", markdownWithTitle("Doc"))
err := Run(context.Background(), trackingConfig(server, filepath.Join(dir, "*.md")))
require.Error(t, err)
assert.Contains(t, err.Error(), "unable to identify the Confluence platform")
assert.Equal(t, 0, countPagesTitled(t, server, "Doc"))
}
// TestRunPublishesWhenTheUserNamesNoPlatform: only a failed request stops the
// run; a user with neither an accountId nor a username is taken for Server.
func TestRunPublishesWhenTheUserNamesNoPlatform(t *testing.T) {
server, _ := docsSpace(t)
server.SetFail(func(r *http.Request) (int, string, bool) {
if r.URL.Path == "/rest/api/user/current" {
return http.StatusOK, `{"type":"anonymous"}`, true
}
return 0, "", false
})
dir := t.TempDir()
writeFile(t, dir, "doc.md", markdownWithTitle("Doc"))
require.NoError(t, Run(context.Background(), trackingConfig(server, filepath.Join(dir, "*.md"))))
assert.Equal(t, 1, countPagesTitled(t, server, "Doc"))
}
func TestCompileOnlyDoesNotAskForThePlatform(t *testing.T) {
server, _ := docsSpace(t)
dir := t.TempDir()
writeFile(t, dir, "doc.md", markdownWithTitle("Doc"))
config := trackingConfig(server, filepath.Join(dir, "*.md"))
config.Tracking.TrackPages = false
config.Mode.CompileOnly = true
require.NoError(t, Run(context.Background(), config))
assert.Zero(t, server.CountRequests("GET", "/rest/api/user/current"))
}
// TestPublishThroughTheGateway is issue #917 end to end: a scoped API token
// goes through the api.atlassian.com gateway, which refuses every v1 endpoint
// to it. A publish and a republish have to get by on v2 alone.
func TestPublishThroughTheGateway(t *testing.T) {
server := confluencetest.New(t)
home := server.AddPage("DOCS", "Home", "page", "")
server.SetHomepage("DOCS", home.ID)
server.SetFail(func(r *http.Request) (int, string, bool) {
if strings.Contains(r.URL.Path, "/rest/api/") {
return http.StatusUnauthorized,
`{"code":401,"message":"Unauthorized; scope does not match"}`, true
}
return 0, "", false
})
baseURL := server.URL + "/ex/confluence/cloud-id"
dir := t.TempDir()
writeFile(t, dir, "guide.md",
"<!-- Space: DOCS -->\n<!-- Title: Guide -->\n\nA guide.\n")
config := Config{
PublishConfig: PublishConfig{
Rendering: RenderingConfig{
Features: []string{"mention"},
},
Output: io.Discard,
},
Files: filepath.Join(dir, "*.md"),
Connection: ConnectionConfig{
BaseURL: baseURL,
Username: "user",
Password: "token",
},
}
require.NoError(t, Run(context.Background(), config))
writeFile(t, dir, "guide.md",
"<!-- Space: DOCS -->\n<!-- Title: Guide -->\n\nA revised guide.\n")
require.NoError(t, Run(context.Background(), config))
api := confluence.NewAPI(baseURL, confluence.Options{Username: "user", Password: "token"})
page, err := api.FindPage(context.Background(), "DOCS", "Guide", "page")
require.NoError(t, err)
require.NotNil(t, page)
stored := server.Page(page.ID)
require.NotNil(t, stored)
assert.Contains(t, stored.Body, "A revised guide.")
// A first publish creates the page and then fills it in, so the second
// run's update is the third version.
assert.Equal(t, int64(3), stored.Version, "the second run updates the page it made")
assert.Equal(t, home.ID, stored.ParentID, "a republish must not move the page")
}
// TestCancellingTheCloudProbeIsNotAFolderRefusal: on a host that does not name
// itself Cloud, a file with folders asks the server which platform it is. A
// cancellation that cuts that question short is not "not Cloud": failing the
// file saying folders need Cloud would be wrong, and not an error errors.Is
// recognises as the cancellation Publish promises.
func TestCancellingTheCloudProbeIsNotAFolderRefusal(t *testing.T) {
server, api := docsSpace(t)
dir := t.TempDir()
file := writeFile(t, dir, "doc.md",
"<!-- Space: DOCS -->\n<!-- Folder: Manuals -->\n<!-- Title: Doc -->\n\nBody.\n")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// The probe is held until the client gives up on it, and the run is
// cancelled once it has arrived.
blocked := make(chan struct{})
var once sync.Once
server.SetFail(func(r *http.Request) (int, string, bool) {
if !strings.HasSuffix(strings.TrimSuffix(r.URL.Path, "/"), "/rest/api/user/current") {
return 0, "", false
}
once.Do(func() { close(blocked) })
<-r.Context().Done()
return http.StatusServiceUnavailable, `{"message":"too late"}`, true
})
go func() {
<-blocked
cancel()
}()
_, err := publishOne(ctx, file, api, publishConfig(server.URL, file).PublishConfig)
require.ErrorIs(t, err, context.Canceled)
assert.NotContains(t, err.Error(), "only available on Confluence Cloud")
}