Skip to content

Commit

Permalink
Add restapi test for timer
Browse files Browse the repository at this point in the history
  • Loading branch information
hanzei committed Nov 10, 2022
1 parent 7ce35a6 commit f01124b
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 1 deletion.
12 changes: 12 additions & 0 deletions test/restapitest/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ func (th *Helper) InstallAppWithCleanup(app *goapp.App) {
call: th.Call,
}

var userClientApp *appclient.Client
app.HandleCall("/internal/get_token/user", func(cr goapp.CallRequest) apps.CallResponse {
userClientApp = cr.AsActingUser()
return apps.NewTextResponse("OK")
})

c := apps.NewCall("/internal/get_token/user").ExpandActingUserClient()
_ = th.asUser.happyCall(app.Manifest.AppID, apps.CallRequest{Call: *c})

require.NotNil(th.T, userClientApp)
th.UserClientApp = userClientApp

th.asUser2 = appClient{
name: "user2",
expectedActingUser: th.ServerTestHelper.BasicUser2,
Expand Down
2 changes: 2 additions & 0 deletions test/restapitest/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type Helper struct {
SystemAdminClientPP *appclient.ClientPP
LocalClientPP *appclient.ClientPP

UserClientApp *appclient.Client

asBot appClient
asUser appClient
asUser2 appClient
Expand Down
1 change: 0 additions & 1 deletion test/restapitest/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ func newKVApp(t testing.TB) *goapp.App {
return app
}

//nolint:golint,unparam
func kvCall(th *Helper, path string, asBot bool, prefix, key string, value interface{}) apps.CallResponse {
creq := apps.CallRequest{
Call: *apps.NewCall(path),
Expand Down
1 change: 1 addition & 0 deletions test/restapitest/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestRESTAPI(t *testing.T) {
"KV": testKV,
"OAuth2": testOAuth2,
"subscriptions": testSubscriptions,
"timer": testTimer,
"notify": testNotify,
"uninstall": testUninstall,
"misc": testMisc,
Expand Down
107 changes: 107 additions & 0 deletions test/restapitest/timer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

package restapitest

import (
"sync"
"testing"
"time"

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

"github.com/mattermost/mattermost-server/v6/api4"

"github.com/mattermost/mattermost-plugin-apps/apps"
"github.com/mattermost/mattermost-plugin-apps/apps/goapp"
)

const timerID = apps.AppID("timer_test")

func newTimerApp(_ testing.TB) *goapp.App {
app := goapp.MakeAppOrPanic(
apps.Manifest{
AppID: timerID,
Version: "v1.1.0",
DisplayName: "tests app timers",
HomepageURL: "https://github.com/mattermost/mattermost-plugin-apps/test/restapitest",
RequestedPermissions: []apps.Permission{
apps.PermissionActAsBot,
apps.PermissionActAsUser,
},
},
)

return app
}

func testTimer(th *Helper) {
app := newTimerApp(th.T)
th.InstallAppWithCleanup(app)

baseTimer := apps.Timer{
Call: apps.Call{
Path: "/timer/execute",
},
}

th.Run("Unauthenticated requests are rejected", func(th *Helper) {
assert := assert.New(th)
client := th.CreateUnauthenticatedClientPP()

resp, err := client.CreateTimer(&baseTimer)
assert.Error(err)
api4.CheckUnauthorizedStatus(th, resp)
})

th.Run("Invalid requests are rejected", func(th *Helper) {
assert := assert.New(th)
client := th.UserClientApp

// No call defined
err := client.CreateTimer(&apps.Timer{})
assert.Error(err)

t := baseTimer

// Negative time
t.At = -100
err = client.CreateTimer(&t)
assert.Error(err)

// at is now
t.At = time.Now().UnixMilli()
err = client.CreateTimer(&t)
assert.Error(err)

// at is less then a second in the future
t.At = time.Now().Add(500 * time.Millisecond).UnixMilli()
err = client.CreateTimer(&t)
assert.Error(err)
})

th.Run("Assert that timer is called", func(th *Helper) {
assert := assert.New(th)
client := th.UserClientApp

var mut sync.Mutex
var called bool
app.HandleCall("/timer/execute",
func(creq goapp.CallRequest) apps.CallResponse {
mut.Lock()
defer mut.Unlock()
called = true
return apps.NewTextResponse("OK")
})

t := baseTimer

t.At = time.Now().Add(2 * time.Second).UnixMilli()
err := client.CreateTimer(&t)
assert.NoError(err)

// Check if callback was called
require.Eventually(th, func() bool { mut.Lock(); defer mut.Unlock(); return called }, 10*time.Second, 50*time.Millisecond)
})
}

0 comments on commit f01124b

Please sign in to comment.