|
| 1 | +// Copyright 2019 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 | + "net/url" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + auth_model "code.gitea.io/gitea/models/auth" |
| 15 | + api "code.gitea.io/gitea/modules/structs" |
| 16 | + |
| 17 | + "github.com/stretchr/testify/assert" |
| 18 | +) |
| 19 | + |
| 20 | +func getCreateProfileReadmeFileOptions(profileType string) api.CreateFileOptions { |
| 21 | + content := fmt.Sprintf("# %s", profileType) |
| 22 | + contentEncoded := base64.StdEncoding.EncodeToString([]byte(content)) |
| 23 | + return api.CreateFileOptions{ |
| 24 | + FileOptions: api.FileOptions{ |
| 25 | + BranchName: "main", |
| 26 | + NewBranchName: "main", |
| 27 | + Message: "create the profile README.md", |
| 28 | + Dates: api.CommitDateOptions{ |
| 29 | + Author: time.Unix(946684810, 0), |
| 30 | + Committer: time.Unix(978307190, 0), |
| 31 | + }, |
| 32 | + }, |
| 33 | + ContentBase64: contentEncoded, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func createTestProfile(t *testing.T, orgName, profileType string) { |
| 38 | + repoName := ".profile" |
| 39 | + isPrivate := false |
| 40 | + if profileType == "Private" { |
| 41 | + repoName = ".profile-private" |
| 42 | + isPrivate = true |
| 43 | + } |
| 44 | + |
| 45 | + ctx := NewAPITestContext(t, "user1", repoName, auth_model.AccessTokenScopeAll) |
| 46 | + session := loginUser(t, "user1") |
| 47 | + tokenAdmin := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeAll) |
| 48 | + |
| 49 | + // create repo |
| 50 | + t.Run("CreateOrganization"+profileType+"ProfileRepo", doAPICreateOrganizationRepository(ctx, orgName, &api.CreateRepoOption{ |
| 51 | + Name: repoName, |
| 52 | + Private: isPrivate, |
| 53 | + })) |
| 54 | + |
| 55 | + // create readme |
| 56 | + createFileOptions := getCreateProfileReadmeFileOptions(profileType) |
| 57 | + req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", orgName, repoName, "README.md"), &createFileOptions). |
| 58 | + AddTokenAuth(tokenAdmin) |
| 59 | + MakeRequest(t, req, http.StatusCreated) |
| 60 | +} |
| 61 | + |
| 62 | +func TestOrgProfile(t *testing.T) { |
| 63 | + onGiteaRun(t, testOrgProfile) |
| 64 | +} |
| 65 | + |
| 66 | +func testOrgProfile(t *testing.T, u *url.URL) { |
| 67 | + // html #user-content-public (markdown title of public profile) |
| 68 | + // html #user-content-private (markdown title of private profile) |
| 69 | + // html #profile_view_as_dropdown (indicate whether the view as dropdown menu is present) |
| 70 | + |
| 71 | + // PART 1: Test Both Private and Public |
| 72 | + createTestProfile(t, "org3", "Public") |
| 73 | + createTestProfile(t, "org3", "Private") |
| 74 | + |
| 75 | + // Anonymous User |
| 76 | + req := NewRequest(t, "GET", "org3") |
| 77 | + resp := MakeRequest(t, req, http.StatusOK) |
| 78 | + htmlDoc := NewHTMLParser(t, resp.Body) |
| 79 | + |
| 80 | + profileDivs := htmlDoc.doc.Find("#user-content-public") |
| 81 | + assert.EqualValues(t, 1, profileDivs.Length()) |
| 82 | + |
| 83 | + dropDownDiv := htmlDoc.doc.Find("#profile_view_as_dropdown") |
| 84 | + assert.EqualValues(t, 0, dropDownDiv.Length()) |
| 85 | + |
| 86 | + // Logged in but not member |
| 87 | + session := loginUser(t, "user24") |
| 88 | + req = NewRequest(t, "GET", "org3") |
| 89 | + resp = session.MakeRequest(t, req, http.StatusOK) |
| 90 | + htmlDoc = NewHTMLParser(t, resp.Body) |
| 91 | + |
| 92 | + profileDivs = htmlDoc.doc.Find("#user-content-public") |
| 93 | + assert.EqualValues(t, 1, profileDivs.Length()) |
| 94 | + |
| 95 | + dropDownDiv = htmlDoc.doc.Find("#profile_view_as_dropdown") |
| 96 | + assert.EqualValues(t, 0, dropDownDiv.Length()) |
| 97 | + |
| 98 | + // Site Admin |
| 99 | + session = loginUser(t, "user1") |
| 100 | + req = NewRequest(t, "GET", "org3") |
| 101 | + resp = session.MakeRequest(t, req, http.StatusOK) |
| 102 | + htmlDoc = NewHTMLParser(t, resp.Body) |
| 103 | + |
| 104 | + profileDivs = htmlDoc.doc.Find("#user-content-public") |
| 105 | + assert.EqualValues(t, 1, profileDivs.Length()) |
| 106 | + |
| 107 | + dropDownDiv = htmlDoc.doc.Find("#profile_view_as_dropdown") |
| 108 | + assert.EqualValues(t, 1, dropDownDiv.Length()) |
| 109 | + |
| 110 | + req = NewRequest(t, "GET", "/org3?view_as=member") |
| 111 | + resp = session.MakeRequest(t, req, http.StatusOK) |
| 112 | + htmlDoc = NewHTMLParser(t, resp.Body) |
| 113 | + |
| 114 | + profileDivs = htmlDoc.doc.Find("#user-content-private") |
| 115 | + assert.EqualValues(t, 1, profileDivs.Length()) |
| 116 | + |
| 117 | + req = NewRequest(t, "GET", "/org3?view_as=public") |
| 118 | + resp = session.MakeRequest(t, req, http.StatusOK) |
| 119 | + htmlDoc = NewHTMLParser(t, resp.Body) |
| 120 | + |
| 121 | + profileDivs = htmlDoc.doc.Find("#user-content-public") |
| 122 | + assert.EqualValues(t, 1, profileDivs.Length()) |
| 123 | + |
| 124 | + // PART 2: Each org has either one of private pr public profile |
| 125 | + createTestProfile(t, "org41", "Public") |
| 126 | + createTestProfile(t, "org42", "Private") |
| 127 | + |
| 128 | + // Anonymous User |
| 129 | + req = NewRequest(t, "GET", "/org41") |
| 130 | + resp = MakeRequest(t, req, http.StatusOK) |
| 131 | + htmlDoc = NewHTMLParser(t, resp.Body) |
| 132 | + profileDivs = htmlDoc.doc.Find("#user-content-public") |
| 133 | + assert.EqualValues(t, 1, profileDivs.Length()) |
| 134 | + dropDownDiv = htmlDoc.doc.Find("#profile_view_as_dropdown") |
| 135 | + assert.EqualValues(t, 0, dropDownDiv.Length()) |
| 136 | + |
| 137 | + req = NewRequest(t, "GET", "/org42") |
| 138 | + resp = MakeRequest(t, req, http.StatusOK) |
| 139 | + htmlDoc = NewHTMLParser(t, resp.Body) |
| 140 | + profileDivs = htmlDoc.doc.Find("#user-content-public") |
| 141 | + assert.EqualValues(t, 0, profileDivs.Length()) |
| 142 | + profileDivs = htmlDoc.doc.Find("#user-content-public") |
| 143 | + assert.EqualValues(t, 0, profileDivs.Length()) |
| 144 | + dropDownDiv = htmlDoc.doc.Find("#profile_view_as_dropdown") |
| 145 | + assert.EqualValues(t, 0, dropDownDiv.Length()) |
| 146 | + |
| 147 | + // Site Admin |
| 148 | + req = NewRequest(t, "GET", "/org41") |
| 149 | + resp = session.MakeRequest(t, req, http.StatusOK) |
| 150 | + htmlDoc = NewHTMLParser(t, resp.Body) |
| 151 | + profileDivs = htmlDoc.doc.Find("#user-content-public") |
| 152 | + assert.EqualValues(t, 1, profileDivs.Length()) |
| 153 | + dropDownDiv = htmlDoc.doc.Find("#profile_view_as_dropdown") |
| 154 | + assert.EqualValues(t, 0, dropDownDiv.Length()) |
| 155 | + |
| 156 | + req = NewRequest(t, "GET", "/org42") |
| 157 | + resp = session.MakeRequest(t, req, http.StatusOK) |
| 158 | + htmlDoc = NewHTMLParser(t, resp.Body) |
| 159 | + profileDivs = htmlDoc.doc.Find("#user-content-private") |
| 160 | + assert.EqualValues(t, 1, profileDivs.Length()) |
| 161 | + dropDownDiv = htmlDoc.doc.Find("#profile_view_as_dropdown") |
| 162 | + assert.EqualValues(t, 0, dropDownDiv.Length()) |
| 163 | +} |
0 commit comments