Files
mostlymatter/api4/resolver_test.go
Agniva De Sarker 3f52bd197a GraphQL (part 2): Model changes and resolvers (#19486)
This PR adds the necessary resolvers to support
the graphQL front-end. Some additional methods
need to be added to the model structs to support
this. Comments have been made to clarify their purpose.

There is a slight duplication of code in the api layer.
But eventually that's going to go away when we move
away from the REST API entirely.

The schema is in api4/schema.graphqls and gets compiled
into the binary as an asset.

The GraphiQL editor url is at GET /api/v5/graphql.

Everything is behind the feature flag MM_FEATUREFLAGS_GRAPHQL.

```release-note
NONE
```
2022-02-11 12:37:05 +05:30

146 строки
3.2 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package api4
import (
"encoding/json"
"os"
"testing"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGraphQLConfig(t *testing.T) {
os.Setenv("MM_FEATUREFLAGS_GRAPHQL", "true")
defer os.Unsetenv("MM_FEATUREFLAGS_GRAPHQL")
th := Setup(t)
th.LoginBasicWithGraphQL()
defer th.TearDown()
var q struct {
Config map[string]string `json:"config"`
}
input := graphQLInput{
OperationName: "config",
Query: `
query config {
config
}
`,
}
cfg, _, err := th.Client.GetOldClientConfig("")
require.NoError(t, err)
resp, err := th.MakeGraphQLRequest(&input)
require.NoError(t, err)
require.Len(t, resp.Errors, 0)
require.NoError(t, json.Unmarshal(resp.Data, &q))
assert.Equal(t, cfg, q.Config)
}
func TestGraphQLLicense(t *testing.T) {
os.Setenv("MM_FEATUREFLAGS_GRAPHQL", "true")
defer os.Unsetenv("MM_FEATUREFLAGS_GRAPHQL")
th := Setup(t)
th.LoginBasicWithGraphQL()
defer th.TearDown()
var q struct {
License map[string]string `json:"license"`
}
input := graphQLInput{
OperationName: "license",
Query: `
query license {
license
}
`,
}
cfg, _, err := th.Client.GetOldClientLicense("")
require.NoError(t, err)
resp, err := th.MakeGraphQLRequest(&input)
require.NoError(t, err)
require.Len(t, resp.Errors, 0)
require.NoError(t, json.Unmarshal(resp.Data, &q))
assert.Equal(t, cfg, q.License)
}
func TestGraphQLChannelsLeft(t *testing.T) {
os.Setenv("MM_FEATUREFLAGS_GRAPHQL", "true")
defer os.Unsetenv("MM_FEATUREFLAGS_GRAPHQL")
th := Setup(t).InitBasic()
defer th.TearDown()
var q struct {
ChannelsLeft []string `json:"channelsLeft"`
}
t.Run("NotLeft", func(t *testing.T) {
input := graphQLInput{
OperationName: "channelsLeft",
Query: `
query channelsLeft($userId: String = "me", $since: Float = 0.0) {
channelsLeft(userId: $userId, since: $since)
}
`,
}
resp, err := th.MakeGraphQLRequest(&input)
require.NoError(t, err)
require.Len(t, resp.Errors, 0)
require.NoError(t, json.Unmarshal(resp.Data, &q))
assert.Len(t, q.ChannelsLeft, 0)
})
t.Run("Left", func(t *testing.T) {
_, err := th.Client.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser.Id)
require.NoError(t, err)
input := graphQLInput{
OperationName: "channelsLeft",
Query: `
query channelsLeft($userId: String = "me", $since: Float = 0.0) {
channelsLeft(userId: $userId, since: $since)
}
`,
}
resp, err := th.MakeGraphQLRequest(&input)
require.NoError(t, err)
require.Len(t, resp.Errors, 0)
require.NoError(t, json.Unmarshal(resp.Data, &q))
assert.Len(t, q.ChannelsLeft, 1)
})
t.Run("LeftAfterTime", func(t *testing.T) {
input := graphQLInput{
OperationName: "channelsLeft",
Query: `
query channelsLeft($userId: String = "me", $since: Float = 0.0) {
channelsLeft(userId: $userId, since: $since)
}
`,
Variables: map[string]interface{}{
"since": model.GetMillis(),
},
}
resp, err := th.MakeGraphQLRequest(&input)
require.NoError(t, err)
require.Len(t, resp.Errors, 0)
require.NoError(t, json.Unmarshal(resp.Data, &q))
assert.Len(t, q.ChannelsLeft, 0)
})
}