Commit 71858882 authored by Seif Hatem's avatar Seif Hatem Committed by Patrick Rice
Browse files

feat(settings): Add AnonymousSearchesAllowed field support

Changelog: Improvements
parent e4b651ff
Loading
Loading
Loading
Loading
+38 −0
Original line number Diff line number Diff line
//go:build integration

package gitlab_test

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	gitlab "gitlab.com/gitlab-org/api/client-go"
)

func Test_Settings_AnonymousSearchesAllowed_Integration(t *testing.T) {
	client := SetupIntegrationClient(t)
	// get initial settings
	settings, _, err := client.Settings.GetSettings()
	require.NoError(t, err, "Failed to get initial settings")

	// get AnonymousSearchesAllowed value
	originalValue := settings.AnonymousSearchesAllowed

	t.Cleanup(func() {
		// restore original setting after test
		_, _, err := client.Settings.UpdateSettings(&gitlab.UpdateSettingsOptions{
			AnonymousSearchesAllowed: &originalValue,
		})
		require.NoError(t, err, "Failed to restore settings")
	})

	// Toggle the AnonymousSearchesAllowed setting
	newValue := !originalValue
	// update setting with the new value for AnonymousSearchesAllowed
	updatedSettings, _, err := client.Settings.UpdateSettings(&gitlab.UpdateSettingsOptions{
		AnonymousSearchesAllowed: &newValue,
	})
	require.NoError(t, err, "Failed to update settings")
	assert.Equal(t, newValue, updatedSettings.AnonymousSearchesAllowed)
}
+2 −0
Original line number Diff line number Diff line
@@ -67,6 +67,7 @@ type Settings struct {
	AllowLocalRequestsFromWebHooksAndServices             bool                      `json:"allow_local_requests_from_web_hooks_and_services"`
	AllowProjectCreationForGuestAndBelow                  bool                      `json:"allow_project_creation_for_guest_and_below"`
	AllowRunnerRegistrationToken                          bool                      `json:"allow_runner_registration_token"`
	AnonymousSearchesAllowed                              bool                      `json:"anonymous_searches_allowed"`
	ArchiveBuildsInHumanReadable                          string                    `json:"archive_builds_in_human_readable"`
	ASCIIDocMaxIncludes                                   int64                     `json:"asciidoc_max_includes"`
	AssetProxyAllowlist                                   []string                  `json:"asset_proxy_allowlist"`
@@ -547,6 +548,7 @@ type UpdateSettingsOptions struct {
	AllowLocalRequestsFromWebHooksAndServices             *bool                                   `url:"allow_local_requests_from_web_hooks_and_services,omitempty" json:"allow_local_requests_from_web_hooks_and_services,omitempty"`
	AllowProjectCreationForGuestAndBelow                  *bool                                   `url:"allow_project_creation_for_guest_and_below,omitempty" json:"allow_project_creation_for_guest_and_below,omitempty"`
	AllowRunnerRegistrationToken                          *bool                                   `url:"allow_runner_registration_token,omitempty" json:"allow_runner_registration_token,omitempty"`
	AnonymousSearchesAllowed                              *bool                                   `url:"anonymous_searches_allowed,omitempty" json:"anonymous_searches_allowed,omitempty"`
	ArchiveBuildsInHumanReadable                          *string                                 `url:"archive_builds_in_human_readable,omitempty" json:"archive_builds_in_human_readable,omitempty"`
	ASCIIDocMaxIncludes                                   *int64                                  `url:"asciidoc_max_includes,omitempty" json:"asciidoc_max_includes,omitempty"`
	AssetProxyAllowlist                                   *[]string                               `url:"asset_proxy_allowlist,omitempty" json:"asset_proxy_allowlist,omitempty"`
+48 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import (
	"testing"

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

func TestGetSettings(t *testing.T) {
@@ -161,3 +162,50 @@ func TestSettings_RequestBody(t *testing.T) {

	assert.Equal(t, want["enforce_ci_inbound_job_token_scope_enabled"], requestBody["enforce_ci_inbound_job_token_scope_enabled"])
}

func TestUpdateSettings_AnonymousSearchesAllowed(t *testing.T) {
	t.Parallel()
	mux, client := setup(t)

	var requestBody map[string]any
	mux.HandleFunc("/api/v4/application/settings", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodPut)

		// Read the request body into `requestBody` by unmarshalling it
		err := json.NewDecoder(r.Body).Decode(&requestBody)
		assert.NoError(t, err)

		fmt.Fprint(w, `{"id":1,   "anonymous_searches_allowed" : true}`)
	})

	_, _, err := client.Settings.UpdateSettings(&UpdateSettingsOptions{
		AnonymousSearchesAllowed: Ptr(true),
	})
	require.NoError(t, err)

	// This is the payload that should be produced. This allows us to test that the request produced matches our options input.
	want := map[string]any{
		"anonymous_searches_allowed": true,
	}

	assert.Equal(t, want["anonymous_searches_allowed"], requestBody["anonymous_searches_allowed"])
}

func TestGetSettings_AnonymousSearchesAllowed(t *testing.T) {
	t.Parallel()
	mux, client := setup(t)

	mux.HandleFunc("/api/v4/application/settings", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodGet)
		fmt.Fprint(w, `{
			"id": 1,
			"default_projects_limit": 100000,
			"anonymous_searches_allowed": true
		}`)
	})

	settings, _, err := client.Settings.GetSettings()
	require.NoError(t, err)

	assert.True(t, settings.AnonymousSearchesAllowed)
}