Files
mostlymatter/server/channels/web/params_test.go
Elias Nahum 7e9cd04a8b Channel Bookmarks (#25449)
* create ChannelBookmarks table

* ChannelBookmark model

* channel bookamrks Store layer

* add GetBookmarksForAllChannelByIdSince

* add channel bookmarks to test store

* Add channel bookmarks to app layer

* remove index for createAt in channel bookmarks migrations

* remove createAt from select channel bookmark query and enable store delete bookmark test

* update reponse of UpdateBookmark

* rename db migration files

* channel bookmarks store update sort order

* channel bookmarks app layer update sort order

* fix lint & tests

* Fix lint and introduce util functions to insert / remove from slice

* remove model etag

* i18n

* defer remove file info after test run

* Fix tests passing the request context

* fix migrations

* fix TestRetry

* Add bookmark permissions (#25560)

* Adds channel bookmarks permissions

* Fix linter

* Remove unnecessary empty lines

* Remove scss change as it's not necessary anymore

* Fix mock store

* Fix mock store and add role entry

* Fix test

* Adds cypress test and update permissions migration to update admin roles

* Adds channel bookmarks roles to default admin roles

* Adds bookmark permissions to default role permissions constant in webapp

* Update mmctl test

* Update permission test after normalising the roles

* fix store tests

* fix app layer tests

* Add new bookmark endpoint (#25624)

* Adds channel bookmarks api scaffold and create endpoint

* Applies review comments to the API docs

* Adds websocket test to create channel bookmark

---------

Co-authored-by: Mattermost Build <build@mattermost.com>

* MM-54426 exclude Channel Bookmarks files from data retention (#25656)

* Augment channel APIs to include bookmarks (#25567)

* update files docs for server 9.4

* Adds update channel bookmark endpoint (#25653)

* Adds update channel bookmark sort order endpoint (#25686)

* Adds update channel bookmark endpoint

* Updates edit app method to return the right deleted bookmark and adds tests

* Adds the update channel bookmark sort order endpoint

* Fix repeated test after merge

* Assign right permissions to each test

* Update store and app layer to return specific errors and add tests

* Adds delete channel bookmark endpoint (#25693)

* Updates edit app method to return the right deleted bookmark and adds tests

* Fix repeated test after merge

* Updates edit app method to return the right deleted bookmark and adds tests

* Adds delete channel bookmark endpoint

* Adds list channel bookmarks endpoint (#25700)

* Add channel moderation to bookmarks (#25716)

* fix migrations index

* fix getChannelsForTeamForUser

* fix getChannelsForTeamForUser

* fix bad merge client4

* fix file api with bookmark permission

* add ChannelBookmarks feature flag

* add missing translations

* Set DB column for type as enum

* use custom type for bookmark query using sqlx

* use transaction when saving bookmark

* return NewErrNotFound instead of Sql.ErrNoRows

* use squirrel for IN query

* add a limit of 1K for records in GetBookmarksForAllChannelByIdSince

* UpdateSortOrder with one single query instead of multiple updates

* fix shadow declaration

* fix channel bookmarks permission string definition in admin console

* fix another shadow declaration

* Fix model conversion

* add SplitSliceInChunks

* remove include bookmarks in channels api

* Cap amount of bookmarks per channel

* add etag back to get channels

* feedback review

* update file info when replacing a bookmark file

* return 501 not implemented when the license is not available

* add detail message when getting channel member on bookmark api

* start audit before permission check on create bookmark api

* use require.Eventuallyf for testing WS events

* remove unnecessary log in app layer

* use require instead of assert to avoid panics

* enforce limit when querying bookmarks since

* prevent to create/update bookmark if file is already attached

* fix lint

* delete file when a bookmark is deleted

* Dot allow to set a fileId and a url at the same time to a bookmark

* fix query to delete a file that belongs to a bookmark

* do not patch the bookmark type

* Server side FeatureFlag check (#26145)

* use ff in server, set ff to false

* turn on FF for unit tests

* defer unset FF for unit tests

* turn ff on for testing

* only allow attaching files that were uploaded for bookmark

* Set feature flag off as default

* fix lint

* update email templates as PR failed

* revert templates

* force the assignment of ID when creating a bookmark

* Fix unit tests

---------

Co-authored-by: Miguel de la Cruz <miguel@mcrx.me>
Co-authored-by: Mattermost Build <build@mattermost.com>
Co-authored-by: Caleb Roseland <caleb@calebroseland.com>
Co-authored-by: Scott Bishel <scott.bishel@mattermost.com>
2024-03-12 08:36:05 -06:00

530 строки
11 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package web
import (
"net/http"
"net/url"
"testing"
"github.com/gorilla/mux"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost/server/public/model"
)
func TestGetPerPageFromQuery(t *testing.T) {
t.Run("defaults should be set", func(t *testing.T) {
query := make(url.Values)
perPage := getPerPageFromQuery(query)
require.Equal(t, PerPageDefault, perPage)
})
t.Run("per_page should take priority", func(t *testing.T) {
query := make(url.Values)
query.Add("pageSize", "100")
query.Add("per_page", "50")
perPage := getPerPageFromQuery(query)
require.Equal(t, 50, perPage)
})
t.Run("pageSize should be used only if per_page is incorrectly set", func(t *testing.T) {
query := make(url.Values)
query.Add("pageSize", "100")
query.Add("per_page", "BAD VALUE")
perPage := getPerPageFromQuery(query)
require.Equal(t, 100, perPage)
})
}
func TestParamsFromRequest(t *testing.T) {
testCases := []struct {
Description string
URL *url.URL
Vars map[string]string
Params *Params
}{
{
"empty params",
mustURL("/"),
nil,
&Params{
PerPage: PerPageDefault,
LogsPerPage: LogsPerPageDefault,
LimitAfter: LimitDefault,
LimitBefore: LimitDefault,
},
},
{
"query params",
mustURL("/page?" +
"channel_id=abc123&" +
"filename=file.ext&" +
"page=42&" +
"time_range=then-till-now&" +
"permanent=1&" +
"logs_per_page=5&" +
"limit_after=6&" +
"limit_before=7&" +
"q=picard&" +
"is_linked=t&" +
"is_configured=TRUE&" +
"not_associated_to_team=this_team&" +
"not_associated_to_channel=this_channel&" +
"filter_allow_reference=true&" +
"filter_parent_team_permitted=True&" +
"paginate=T&" +
"include_member_count=1&" +
"not_associated_to_group=test&" +
"exclude_default_channels=1&" +
"group_ids=hello,world&" +
"include_total_count=T&" +
"include_deleted=True&" +
"exclude_policy_constrained=TRUE&" +
"filter_has_member=xyz"),
nil,
&Params{
ChannelId: "abc123",
Filename: "file.ext",
Page: 42,
TimeRange: "then-till-now",
PerPage: PerPageDefault,
Permanent: true,
LogsPerPage: 5,
LimitAfter: 6,
LimitBefore: 7,
Q: "picard",
IsLinked: boolPtr(true),
IsConfigured: boolPtr(true),
NotAssociatedToTeam: "this_team",
NotAssociatedToChannel: "this_channel",
FilterAllowReference: true,
FilterParentTeamPermitted: true,
Paginate: boolPtr(true),
IncludeMemberCount: true,
NotAssociatedToGroup: "test",
ExcludeDefaultChannels: true,
GroupIDs: "hello,world",
IncludeTotalCount: true,
IncludeDeleted: true,
ExcludePolicyConstrained: true,
FilterHasMember: "xyz",
},
},
{
"page invalid",
mustURL("?page=hello"),
nil,
&Params{
Page: PageDefault,
PerPage: PerPageDefault,
LogsPerPage: LogsPerPageDefault,
LimitAfter: LimitDefault,
LimitBefore: LimitDefault,
},
},
{
"page negative",
mustURL("?page=-1"),
nil,
&Params{
Page: PageDefault,
PerPage: PerPageDefault,
LogsPerPage: LogsPerPageDefault,
LimitAfter: LimitDefault,
LimitBefore: LimitDefault,
},
},
{
"per page valid",
mustURL("?per_page=123"),
nil,
&Params{
PerPage: 123,
LogsPerPage: LogsPerPageDefault,
LimitAfter: LimitDefault,
LimitBefore: LimitDefault,
},
},
{
"per page too small",
mustURL("?per_page=-100"),
nil,
&Params{
PerPage: PerPageDefault,
LogsPerPage: LogsPerPageDefault,
LimitAfter: LimitDefault,
LimitBefore: LimitDefault,
},
},
{
"per page too big",
mustURL("?per_page=100000"),
nil,
&Params{
PerPage: PerPageMaximum,
LogsPerPage: LogsPerPageDefault,
LimitAfter: LimitDefault,
LimitBefore: LimitDefault,
},
},
{
"logs per page valid",
mustURL("?logs_per_page=512"),
nil,
&Params{
LogsPerPage: 512,
PerPage: PerPageDefault,
LimitAfter: LimitDefault,
LimitBefore: LimitDefault,
},
},
{
"logs per page invalid",
mustURL("?logs_per_page=logs"),
nil,
&Params{
LogsPerPage: LogsPerPageDefault,
PerPage: PerPageDefault,
LimitAfter: LimitDefault,
LimitBefore: LimitDefault,
},
},
{
"logs per page too small",
mustURL("?logs_per_page=-512"),
nil,
&Params{
LogsPerPage: LogsPerPageDefault,
PerPage: PerPageDefault,
LimitAfter: LimitDefault,
LimitBefore: LimitDefault,
},
},
{
"logs per page too big",
mustURL("?logs_per_page=99999999"),
nil,
&Params{
LogsPerPage: LogsPerPageMaximum,
PerPage: PerPageDefault,
LimitAfter: LimitDefault,
LimitBefore: LimitDefault,
},
},
{
"limit before valid",
mustURL("?limit_before=100"),
nil,
&Params{
LimitBefore: 100,
PerPage: PerPageDefault,
LogsPerPage: LogsPerPageDefault,
LimitAfter: LimitDefault,
},
},
{
"limit before invalid",
mustURL("?limit_before=limit"),
nil,
&Params{
LimitBefore: LimitDefault,
PerPage: PerPageDefault,
LogsPerPage: LogsPerPageDefault,
LimitAfter: LimitDefault,
},
},
{
"limit before too small",
mustURL("?limit_before=-100"),
nil,
&Params{
LimitBefore: LimitDefault,
PerPage: PerPageDefault,
LogsPerPage: LogsPerPageDefault,
LimitAfter: LimitDefault,
},
},
{
"limit before too big",
mustURL("?limit_before=99999"),
nil,
&Params{
LimitBefore: LimitMaximum,
PerPage: PerPageDefault,
LogsPerPage: LogsPerPageDefault,
LimitAfter: LimitDefault,
},
},
{
"limit after valid",
mustURL("?limit_after=100"),
nil,
&Params{
LimitAfter: 100,
PerPage: PerPageDefault,
LogsPerPage: LogsPerPageDefault,
LimitBefore: LimitDefault,
},
},
{
"limit after invalid",
mustURL("?limit_after=limit"),
nil,
&Params{
LimitAfter: LimitDefault,
PerPage: PerPageDefault,
LogsPerPage: LogsPerPageDefault,
LimitBefore: LimitDefault,
},
},
{
"limit after too small",
mustURL("?limit_aftere=-100"),
nil,
&Params{
LimitAfter: LimitDefault,
PerPage: PerPageDefault,
LogsPerPage: LogsPerPageDefault,
LimitBefore: LimitDefault,
},
},
{
"limit after too big",
mustURL("?limit_after=99999"),
nil,
&Params{
LimitAfter: LimitMaximum,
PerPage: PerPageDefault,
LogsPerPage: LogsPerPageDefault,
LimitBefore: LimitDefault,
},
},
{
"group source custom",
mustURL("?group_source=custom"),
nil,
&Params{
GroupSource: model.GroupSourceCustom,
PerPage: PerPageDefault,
LogsPerPage: LogsPerPageDefault,
LimitBefore: LimitDefault,
LimitAfter: LimitDefault,
},
},
{
"group source LDAP",
mustURL("?group_source=ldap"),
nil,
&Params{
GroupSource: model.GroupSourceLdap,
PerPage: PerPageDefault,
LogsPerPage: LogsPerPageDefault,
LimitBefore: LimitDefault,
LimitAfter: LimitDefault,
},
},
{
"group source other",
mustURL("?group_source=aabbcc"),
nil,
&Params{
GroupSource: model.GroupSourceLdap,
PerPage: PerPageDefault,
LogsPerPage: LogsPerPageDefault,
LimitBefore: LimitDefault,
LimitAfter: LimitDefault,
},
},
{
"group source empty",
mustURL("?group_souce="),
nil,
&Params{
GroupSource: "",
PerPage: PerPageDefault,
LogsPerPage: LogsPerPageDefault,
LimitBefore: LimitDefault,
LimitAfter: LimitDefault,
},
},
{
"timestamp valid",
mustURL("/"),
map[string]string{
"timestamp": "1234567",
},
&Params{
Timestamp: 1234567,
PerPage: PerPageDefault,
LogsPerPage: LogsPerPageDefault,
LimitBefore: LimitDefault,
LimitAfter: LimitDefault,
},
},
{
"timestamp valid",
mustURL("/"),
map[string]string{
"timestamp": "yes",
},
&Params{
Timestamp: 0,
PerPage: PerPageDefault,
LogsPerPage: LogsPerPageDefault,
LimitBefore: LimitDefault,
LimitAfter: LimitDefault,
},
},
{
"timestamp too small",
mustURL("/"),
map[string]string{
"timestamp": "-1234567",
},
&Params{
Timestamp: 0,
PerPage: PerPageDefault,
LogsPerPage: LogsPerPageDefault,
LimitBefore: LimitDefault,
LimitAfter: LimitDefault,
},
},
{
"syncable type teams",
mustURL("/"),
map[string]string{
"syncable_type": "teams",
},
&Params{
SyncableType: model.GroupSyncableTypeTeam,
PerPage: PerPageDefault,
LogsPerPage: LogsPerPageDefault,
LimitBefore: LimitDefault,
LimitAfter: LimitDefault,
},
},
{
"syncable type channels",
mustURL("/"),
map[string]string{
"syncable_type": "channels",
},
&Params{
SyncableType: model.GroupSyncableTypeChannel,
PerPage: PerPageDefault,
LogsPerPage: LogsPerPageDefault,
LimitBefore: LimitDefault,
LimitAfter: LimitDefault,
},
},
{
"syncable type other",
mustURL("/"),
map[string]string{
"syncable_type": "unknownvalue",
},
&Params{
SyncableType: "",
PerPage: PerPageDefault,
LogsPerPage: LogsPerPageDefault,
LimitBefore: LimitDefault,
LimitAfter: LimitDefault,
},
},
{
"include channel bookmarks",
mustURL("/?include_bookmarks=true"),
nil,
&Params{
BookmarksSince: 0,
LimitAfter: LimitDefault,
PerPage: PerPageDefault,
LogsPerPage: LogsPerPageDefault,
LimitBefore: LimitDefault,
},
},
{
"include channel bookmarks with negative bookmark since",
mustURL("/?include_bookmarks=true&bookmarks_since=-1"),
nil,
&Params{
BookmarksSince: 0,
LimitAfter: LimitDefault,
PerPage: PerPageDefault,
LogsPerPage: LogsPerPageDefault,
LimitBefore: LimitDefault,
},
},
{
"include channel bookmarks with bookmark since",
mustURL("/?include_bookmarks=true&bookmarks_since=123456789"),
nil,
&Params{
BookmarksSince: 123456789,
LimitAfter: LimitDefault,
PerPage: PerPageDefault,
LogsPerPage: LogsPerPageDefault,
LimitBefore: LimitDefault,
},
},
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.Description, func(t *testing.T) {
t.Parallel()
r := &http.Request{URL: testCase.URL}
r = mux.SetURLVars(r, testCase.Vars)
params := ParamsFromRequest(r)
require.Equal(t, testCase.Params, params)
})
}
}
func mustURL(u string) *url.URL {
parsed, err := url.Parse(u)
if err != nil {
panic(err)
}
return parsed
}
func boolPtr(b bool) *bool {
return &b
}