code.gitea.io/gitea@v1.21.7/tests/integration/api_wiki_test.go (about) 1 // Copyright 2021 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package integration 5 6 import ( 7 "encoding/base64" 8 "fmt" 9 "net/http" 10 "testing" 11 12 auth_model "code.gitea.io/gitea/models/auth" 13 api "code.gitea.io/gitea/modules/structs" 14 "code.gitea.io/gitea/tests" 15 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func TestAPIGetWikiPage(t *testing.T) { 20 defer tests.PrepareTestEnv(t)() 21 22 username := "user2" 23 24 urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/wiki/page/Home", username, "repo1") 25 26 req := NewRequest(t, "GET", urlStr) 27 resp := MakeRequest(t, req, http.StatusOK) 28 var page *api.WikiPage 29 DecodeJSON(t, resp, &page) 30 31 assert.Equal(t, &api.WikiPage{ 32 WikiPageMetaData: &api.WikiPageMetaData{ 33 Title: "Home", 34 HTMLURL: page.HTMLURL, 35 SubURL: "Home", 36 LastCommit: &api.WikiCommit{ 37 ID: "2c54faec6c45d31c1abfaecdab471eac6633738a", 38 Author: &api.CommitUser{ 39 Identity: api.Identity{ 40 Name: "Ethan Koenig", 41 Email: "ethantkoenig@gmail.com", 42 }, 43 Date: "2017-11-27T04:31:18Z", 44 }, 45 Committer: &api.CommitUser{ 46 Identity: api.Identity{ 47 Name: "Ethan Koenig", 48 Email: "ethantkoenig@gmail.com", 49 }, 50 Date: "2017-11-27T04:31:18Z", 51 }, 52 Message: "Add Home.md\n", 53 }, 54 }, 55 ContentBase64: base64.RawStdEncoding.EncodeToString( 56 []byte("# Home page\n\nThis is the home page!\n"), 57 ), 58 CommitCount: 1, 59 Sidebar: "", 60 Footer: "", 61 }, page) 62 } 63 64 func TestAPIListWikiPages(t *testing.T) { 65 defer tests.PrepareTestEnv(t)() 66 67 username := "user2" 68 69 urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/wiki/pages", username, "repo1") 70 71 req := NewRequest(t, "GET", urlStr) 72 resp := MakeRequest(t, req, http.StatusOK) 73 74 var meta []*api.WikiPageMetaData 75 DecodeJSON(t, resp, &meta) 76 77 dummymeta := []*api.WikiPageMetaData{ 78 { 79 Title: "Home", 80 HTMLURL: meta[0].HTMLURL, 81 SubURL: "Home", 82 LastCommit: &api.WikiCommit{ 83 ID: "2c54faec6c45d31c1abfaecdab471eac6633738a", 84 Author: &api.CommitUser{ 85 Identity: api.Identity{ 86 Name: "Ethan Koenig", 87 Email: "ethantkoenig@gmail.com", 88 }, 89 Date: "2017-11-27T04:31:18Z", 90 }, 91 Committer: &api.CommitUser{ 92 Identity: api.Identity{ 93 Name: "Ethan Koenig", 94 Email: "ethantkoenig@gmail.com", 95 }, 96 Date: "2017-11-27T04:31:18Z", 97 }, 98 Message: "Add Home.md\n", 99 }, 100 }, 101 { 102 Title: "Page With Image", 103 HTMLURL: meta[1].HTMLURL, 104 SubURL: "Page-With-Image", 105 LastCommit: &api.WikiCommit{ 106 ID: "0cf15c3f66ec8384480ed9c3cf87c9e97fbb0ec3", 107 Author: &api.CommitUser{ 108 Identity: api.Identity{ 109 Name: "Gabriel Silva Simões", 110 Email: "simoes.sgabriel@gmail.com", 111 }, 112 Date: "2019-01-25T01:41:55Z", 113 }, 114 Committer: &api.CommitUser{ 115 Identity: api.Identity{ 116 Name: "Gabriel Silva Simões", 117 Email: "simoes.sgabriel@gmail.com", 118 }, 119 Date: "2019-01-25T01:41:55Z", 120 }, 121 Message: "Add jpeg.jpg and page with image\n", 122 }, 123 }, 124 { 125 Title: "Page With Spaced Name", 126 HTMLURL: meta[2].HTMLURL, 127 SubURL: "Page-With-Spaced-Name", 128 LastCommit: &api.WikiCommit{ 129 ID: "c10d10b7e655b3dab1f53176db57c8219a5488d6", 130 Author: &api.CommitUser{ 131 Identity: api.Identity{ 132 Name: "Gabriel Silva Simões", 133 Email: "simoes.sgabriel@gmail.com", 134 }, 135 Date: "2019-01-25T01:39:51Z", 136 }, 137 Committer: &api.CommitUser{ 138 Identity: api.Identity{ 139 Name: "Gabriel Silva Simões", 140 Email: "simoes.sgabriel@gmail.com", 141 }, 142 Date: "2019-01-25T01:39:51Z", 143 }, 144 Message: "Add page with spaced name\n", 145 }, 146 }, 147 { 148 Title: "Unescaped File", 149 HTMLURL: meta[3].HTMLURL, 150 SubURL: "Unescaped-File", 151 LastCommit: &api.WikiCommit{ 152 ID: "0dca5bd9b5d7ef937710e056f575e86c0184ba85", 153 Author: &api.CommitUser{ 154 Identity: api.Identity{ 155 Name: "6543", 156 Email: "6543@obermui.de", 157 }, 158 Date: "2021-07-19T16:42:46Z", 159 }, 160 Committer: &api.CommitUser{ 161 Identity: api.Identity{ 162 Name: "6543", 163 Email: "6543@obermui.de", 164 }, 165 Date: "2021-07-19T16:42:46Z", 166 }, 167 Message: "add unescaped file\n", 168 }, 169 }, 170 } 171 172 assert.Equal(t, dummymeta, meta) 173 } 174 175 func TestAPINewWikiPage(t *testing.T) { 176 for _, title := range []string{ 177 "New page", 178 "&&&&", 179 } { 180 defer tests.PrepareTestEnv(t)() 181 username := "user2" 182 session := loginUser(t, username) 183 token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository) 184 185 urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/wiki/new?token=%s", username, "repo1", token) 186 187 req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateWikiPageOptions{ 188 Title: title, 189 ContentBase64: base64.StdEncoding.EncodeToString([]byte("Wiki page content for API unit tests")), 190 Message: "", 191 }) 192 MakeRequest(t, req, http.StatusCreated) 193 } 194 } 195 196 func TestAPIEditWikiPage(t *testing.T) { 197 defer tests.PrepareTestEnv(t)() 198 username := "user2" 199 session := loginUser(t, username) 200 token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository) 201 202 urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/wiki/page/Page-With-Spaced-Name?token=%s", username, "repo1", token) 203 204 req := NewRequestWithJSON(t, "PATCH", urlStr, &api.CreateWikiPageOptions{ 205 Title: "edited title", 206 ContentBase64: base64.StdEncoding.EncodeToString([]byte("Edited wiki page content for API unit tests")), 207 Message: "", 208 }) 209 MakeRequest(t, req, http.StatusOK) 210 } 211 212 func TestAPIListPageRevisions(t *testing.T) { 213 defer tests.PrepareTestEnv(t)() 214 username := "user2" 215 216 urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/wiki/revisions/Home", username, "repo1") 217 218 req := NewRequest(t, "GET", urlStr) 219 resp := MakeRequest(t, req, http.StatusOK) 220 221 var revisions *api.WikiCommitList 222 DecodeJSON(t, resp, &revisions) 223 224 dummyrevisions := &api.WikiCommitList{ 225 WikiCommits: []*api.WikiCommit{ 226 { 227 ID: "2c54faec6c45d31c1abfaecdab471eac6633738a", 228 Author: &api.CommitUser{ 229 Identity: api.Identity{ 230 Name: "Ethan Koenig", 231 Email: "ethantkoenig@gmail.com", 232 }, 233 Date: "2017-11-27T04:31:18Z", 234 }, 235 Committer: &api.CommitUser{ 236 Identity: api.Identity{ 237 Name: "Ethan Koenig", 238 Email: "ethantkoenig@gmail.com", 239 }, 240 Date: "2017-11-27T04:31:18Z", 241 }, 242 Message: "Add Home.md\n", 243 }, 244 }, 245 Count: 1, 246 } 247 248 assert.Equal(t, dummyrevisions, revisions) 249 }