Files
mostlymatter/server/channels/api4/usage_test.go
Claudio Costa 611b2a8e79 [MM-62408] Server Code Coverage with Fully Parallel Tests (#30078)
* TestPool

* Store infra

* Store tests updates

* Bump maximum concurrent postgres connections

* More infra

* channels/jobs

* channels/app

* channels/api4

* Protect i18n from concurrent access

* Replace some use of os.Setenv

* Remove debug

* Lint fixes

* Fix more linting

* Fix test

* Remove use of Setenv in drafts tests

* Fix flaky TestWebHubCloseConnOnDBFail

* Fix merge

* [MM-62408] Add CI job to generate test coverage (#30284)

* Add CI job to generate test coverage

* Remove use of Setenv in drafts tests

* Fix flaky TestWebHubCloseConnOnDBFail

* Fix more Setenv usage

* Fix more potential flakyness

* Remove parallelism from flaky test

* Remove conflicting env var

* Fix

* Disable parallelism

* Test atomic covermode

* Disable parallelism

* Enable parallelism

* Add upload coverage step

* Fix codecov.yml

* Add codecov.yml

* Remove redundant workspace field

* Add Parallel() util methods and refactor

* Fix formatting

* More formatting fixes

* Fix reporting
2025-05-30 13:58:26 +02:00

102 строки
2.7 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package api4
import (
"context"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost/server/public/model"
)
func TestGetPostsUsage(t *testing.T) {
mainHelper.Parallel(t)
t.Run("unauthenticated users can not access", func(t *testing.T) {
th := Setup(t)
defer th.TearDown()
_, err := th.Client.Logout(context.Background())
require.NoError(t, err)
usage, r, err := th.Client.GetPostsUsage(context.Background())
assert.Error(t, err)
assert.Nil(t, usage)
assert.Equal(t, http.StatusUnauthorized, r.StatusCode)
})
t.Run("good request returns response", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
for i := 0; i < 14; i++ {
th.CreatePost()
}
total, err := th.Server.Store().Post().AnalyticsPostCount(&model.PostCountOptions{ExcludeDeleted: true})
require.NoError(t, err)
usersOnly, err := th.Server.Store().Post().AnalyticsPostCount(&model.PostCountOptions{ExcludeDeleted: true, UsersPostsOnly: true})
require.NoError(t, err)
require.GreaterOrEqual(t, usersOnly, int64(14))
require.LessOrEqual(t, usersOnly, int64(20))
require.GreaterOrEqual(t, total, usersOnly)
usage, r, err := th.Client.GetPostsUsage(context.Background())
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, r.StatusCode)
assert.NotNil(t, usage)
assert.Equal(t, int64(10), usage.Count)
})
}
func TestGetStorageUsage(t *testing.T) {
mainHelper.Parallel(t)
t.Run("unauthenticated users cannot access", func(t *testing.T) {
th := Setup(t)
defer th.TearDown()
_, err := th.Client.Logout(context.Background())
require.NoError(t, err)
usage, r, err := th.Client.GetStorageUsage(context.Background())
assert.Error(t, err)
assert.Nil(t, usage)
assert.Equal(t, http.StatusUnauthorized, r.StatusCode)
})
}
func TestGetTeamsUsage(t *testing.T) {
mainHelper.Parallel(t)
t.Run("unauthenticated users can not access", func(t *testing.T) {
th := Setup(t)
defer th.TearDown()
_, err := th.Client.Logout(context.Background())
require.NoError(t, err)
usage, r, err := th.Client.GetTeamsUsage(context.Background())
assert.Error(t, err)
assert.Nil(t, usage)
assert.Equal(t, http.StatusUnauthorized, r.StatusCode)
})
t.Run("good request returns response", func(t *testing.T) {
// Following calls create a total of 3 teams
th := Setup(t).InitBasic()
defer th.TearDown()
th.CreateTeam()
th.CreateTeam()
usage, r, err := th.Client.GetTeamsUsage(context.Background())
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, r.StatusCode)
assert.NotNil(t, usage)
assert.Equal(t, int64(3), usage.Active)
})
}