aboutsummaryrefslogtreecommitdiffstats
path: root/tests/integration/org_profile_test.go
blob: 73cafd85c2b32ec79c9704e898e4123cf47d7c6c (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
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package integration

import (
	"encoding/base64"
	"fmt"
	"net/http"
	"net/url"
	"testing"
	"time"

	auth_model "code.gitea.io/gitea/models/auth"
	api "code.gitea.io/gitea/modules/structs"
	"code.gitea.io/gitea/modules/util"
	"code.gitea.io/gitea/routers/web/shared/user"

	"github.com/stretchr/testify/assert"
)

func getCreateProfileReadmeFileOptions(content string) api.CreateFileOptions {
	contentEncoded := base64.StdEncoding.EncodeToString([]byte(content))
	return api.CreateFileOptions{
		FileOptions: api.FileOptions{
			BranchName:    "main",
			NewBranchName: "main",
			Message:       "create the profile README.md",
			Dates: api.CommitDateOptions{
				Author:    time.Unix(946684810, 0),
				Committer: time.Unix(978307190, 0),
			},
		},
		ContentBase64: contentEncoded,
	}
}

func createTestProfile(t *testing.T, orgName, profileRepoName, readmeContent string) {
	isPrivate := profileRepoName == user.RepoNameProfilePrivate

	ctx := NewAPITestContext(t, "user1", profileRepoName, auth_model.AccessTokenScopeAll)
	session := loginUser(t, "user1")
	tokenAdmin := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeAll)

	// create repo
	doAPICreateOrganizationRepository(ctx, orgName, &api.CreateRepoOption{Name: profileRepoName, Private: isPrivate})(t)

	// create readme
	createFileOptions := getCreateProfileReadmeFileOptions(readmeContent)
	req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", orgName, profileRepoName, "README.md"), &createFileOptions).
		AddTokenAuth(tokenAdmin)
	MakeRequest(t, req, http.StatusCreated)
}

func TestOrgProfile(t *testing.T) {
	onGiteaRun(t, testOrgProfile)
}

func testOrgProfile(t *testing.T, u *url.URL) {
	const contentPublicReadme = "Public Readme Content"
	const contentPrivateReadme = "Private Readme Content"
	// HTML: "#org-home-view-as-dropdown" (indicate whether the view as dropdown menu is present)

	// PART 1: Test Both Private and Public
	createTestProfile(t, "org3", user.RepoNameProfile, contentPublicReadme)
	createTestProfile(t, "org3", user.RepoNameProfilePrivate, contentPrivateReadme)

	// Anonymous User
	req := NewRequest(t, "GET", "org3")
	resp := MakeRequest(t, req, http.StatusOK)
	bodyString := util.UnsafeBytesToString(resp.Body.Bytes())
	assert.Contains(t, bodyString, contentPublicReadme)
	assert.NotContains(t, bodyString, `id="org-home-view-as-dropdown"`)

	// Logged in but not member
	session := loginUser(t, "user24")
	req = NewRequest(t, "GET", "org3")
	resp = session.MakeRequest(t, req, http.StatusOK)
	bodyString = util.UnsafeBytesToString(resp.Body.Bytes())
	assert.Contains(t, bodyString, contentPublicReadme)
	assert.NotContains(t, bodyString, `id="org-home-view-as-dropdown"`)

	// Site Admin
	session = loginUser(t, "user1")
	req = NewRequest(t, "GET", "/org3")
	resp = session.MakeRequest(t, req, http.StatusOK)
	bodyString = util.UnsafeBytesToString(resp.Body.Bytes())
	assert.Contains(t, bodyString, contentPrivateReadme) // as an org member, default to show the private profile
	assert.Contains(t, bodyString, `id="org-home-view-as-dropdown"`)

	req = NewRequest(t, "GET", "/org3?view_as=member")
	resp = session.MakeRequest(t, req, http.StatusOK)
	bodyString = util.UnsafeBytesToString(resp.Body.Bytes())
	assert.Contains(t, bodyString, contentPrivateReadme)
	assert.Contains(t, bodyString, `id="org-home-view-as-dropdown"`)

	req = NewRequest(t, "GET", "/org3?view_as=public")
	resp = session.MakeRequest(t, req, http.StatusOK)
	bodyString = util.UnsafeBytesToString(resp.Body.Bytes())
	assert.Contains(t, bodyString, contentPublicReadme)
	assert.Contains(t, bodyString, `id="org-home-view-as-dropdown"`)

	// PART 2: Each org has either one of private pr public profile
	createTestProfile(t, "org41", user.RepoNameProfile, contentPublicReadme)
	createTestProfile(t, "org42", user.RepoNameProfilePrivate, contentPrivateReadme)

	// Anonymous User
	req = NewRequest(t, "GET", "/org41")
	resp = MakeRequest(t, req, http.StatusOK)
	bodyString = util.UnsafeBytesToString(resp.Body.Bytes())
	assert.Contains(t, bodyString, contentPublicReadme)
	assert.NotContains(t, bodyString, `id="org-home-view-as-dropdown"`)

	req = NewRequest(t, "GET", "/org42")
	resp = MakeRequest(t, req, http.StatusOK)
	bodyString = util.UnsafeBytesToString(resp.Body.Bytes())
	assert.NotContains(t, bodyString, contentPrivateReadme)
	assert.NotContains(t, bodyString, `id="org-home-view-as-dropdown"`)

	// Site Admin
	req = NewRequest(t, "GET", "/org41")
	resp = session.MakeRequest(t, req, http.StatusOK)
	bodyString = util.UnsafeBytesToString(resp.Body.Bytes())
	assert.Contains(t, bodyString, contentPublicReadme)
	assert.NotContains(t, bodyString, `id="org-home-view-as-dropdown"`)

	req = NewRequest(t, "GET", "/org42")
	resp = session.MakeRequest(t, req, http.StatusOK)
	bodyString = util.UnsafeBytesToString(resp.Body.Bytes())
	assert.Contains(t, bodyString, contentPrivateReadme)
	assert.NotContains(t, bodyString, `id="org-home-view-as-dropdown"`)
}