Skip to content

Commit

Permalink
Add e2e tests and refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
vmrajas committed Oct 28, 2020
1 parent 78f7d00 commit 1b781e0
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 31 deletions.
65 changes: 65 additions & 0 deletions graphql/e2e/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,42 @@ func TestRootFilter(t *testing.T) {
}
}

func TestRootCountQuery(t *testing.T) {
testCases := []TestCase{{
user: "user1",
role: "USER",
result: `{"aggregateColumn": {"count": 1}}`,
}, {
user: "user2",
role: "USER",
result: `{"aggregateColumn": {"count": 3}}`,
}, {
user: "user4",
role: "USER",
result: `{"aggregateColumn": {"count": 2}}`,
}}
query := `
query {
aggregateColumn {
count
}
}`

for _, tcase := range testCases {
t.Run(tcase.role+tcase.user, func(t *testing.T) {
getUserParams := &common.GraphQLParams{
Headers: common.GetJWT(t, tcase.user, tcase.role, metaInfo),
Query: query,
}

gqlResponse := getUserParams.ExecuteAsPost(t, graphqlURL)
require.Nil(t, gqlResponse.Errors)

require.JSONEq(t, string(gqlResponse.Data), tcase.result)
})
}
}

func TestDeepRBACValue(t *testing.T) {
testCases := []TestCase{
{user: "user1", role: "USER", result: `{"queryUser": [{"username": "user1", "issues":[]}]}`},
Expand Down Expand Up @@ -930,6 +966,35 @@ func TestRBACFilter(t *testing.T) {
}
}

func TestRBACFilterWithCountQuery(t *testing.T) {
testCases := []TestCase{
{role: "USER", result: `{"aggregateLog": null}`},
{result: `{"aggregateLog": null}`},
{role: "ADMIN", result: `{"aggregateLog": {"count": 2}}`}}

query := `
query {
aggregateLog {
count
}
}
`

for _, tcase := range testCases {
t.Run(tcase.role+tcase.user, func(t *testing.T) {
getUserParams := &common.GraphQLParams{
Headers: common.GetJWT(t, tcase.user, tcase.role, metaInfo),
Query: query,
}

gqlResponse := getUserParams.ExecuteAsPost(t, graphqlURL)
require.Nil(t, gqlResponse.Errors)

require.JSONEq(t, string(gqlResponse.Data), tcase.result)
})
}
}

func TestAndRBACFilter(t *testing.T) {
testCases := []TestCase{{
user: "user1",
Expand Down
3 changes: 3 additions & 0 deletions graphql/e2e/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ func RunAll(t *testing.T) {
t.Run("alias works for queries", queryWithAlias)
t.Run("cascade directive", queryWithCascade)
t.Run("query geo near filter", queryGeoNearFilter)
t.Run("query count without filter", queryCountWithoutFilter)
t.Run("query count with filter", queryCountWithFilter)
t.Run("query count with alias", queryCountWithAlias)

// mutation tests
t.Run("add mutation", addMutation)
Expand Down
62 changes: 62 additions & 0 deletions graphql/e2e/common/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2310,3 +2310,65 @@ func queryGeoNearFilter(t *testing.T) {
// Cleanup
deleteGqlType(t, "Hotel", map[string]interface{}{}, 3, nil)
}

func queryCountWithFilter(t *testing.T) {
queryPostParams := &GraphQLParams{
Query: `query {
aggregatePost (filter: {title : { anyofterms : "Introducing" }} ) {
count
}
}`,
}

gqlResponse := queryPostParams.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, gqlResponse)
testutil.CompareJSON(t,
`{"aggregatePost":{"count":1}}`,
string(gqlResponse.Data))

queryPostParams = &GraphQLParams{
Query: `query {
aggregatePost (filter: {title : { anyofterms : "Nothing" }} ) {
count
}
}`,
}

gqlResponse = queryPostParams.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, gqlResponse)
testutil.CompareJSON(t,
`{"aggregatePost":{"count":0}}`,
string(gqlResponse.Data))
}

func queryCountWithoutFilter(t *testing.T) {
queryPostParams := &GraphQLParams{
Query: `query {
aggregatePost {
count
}
}`,
}

gqlResponse := queryPostParams.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, gqlResponse)
testutil.CompareJSON(t,
`{"aggregatePost":{"count":4}}`,
string(gqlResponse.Data))
}

func queryCountWithAlias(t *testing.T) {
queryPostParams := &GraphQLParams{
Query: `query {
aggregatePost {
cnt: count
}
}`,
}

gqlResponse := queryPostParams.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, gqlResponse)
testutil.CompareJSON(t,
`{"aggregatePost":{"cnt":4}}`,
string(gqlResponse.Data))
}
47 changes: 16 additions & 31 deletions graphql/resolve/query_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,36 +148,11 @@ func aggregateQuery(query schema.Query, authRw *authRewriter) *gql.GraphQuery {
// Get the type which the count query is written for
mainType := query.ConstructedFor()

rbac := authRw.evaluateStaticRules(mainType)
dgQuery := &gql.GraphQuery{
Attr: query.Name(),
}

dgQuery, rbac := addCommonRules(query, mainType, authRw)
if rbac == schema.Negative {
dgQuery.Attr = dgQuery.Attr + "()"
return dgQuery
}

if authRw != nil && (authRw.isWritingAuth || authRw.filterByUid) && (authRw.varName != "" || authRw.parentVarName != "") {
// When rewriting auth rules, they always start like
// Todo2 as var(func: uid(Todo1)) @cascade {
// Where Todo1 is the variable generated from the filter of the field
// we are adding auth to.

authRw.addVariableUIDFunc(dgQuery)
// This is executed when querying while performing delete mutation request since
// in case of delete mutation we already have variable `MutationQueryVar` at root level.
if authRw.filterByUid {
// Since the variable is only added at the top level we reset the `authRW` variables.
authRw.varName = ""
authRw.filterByUid = false
}
} else if ids := idFilter(extractQueryFilter(query), mainType.IDField()); ids != nil {
addUIDFunc(dgQuery, ids)
} else {
addTypeFunc(dgQuery, mainType.Name())
}

// Add filter
filter, _ := query.ArgValue("filter").(map[string]interface{})
_ = addFilter(dgQuery, mainType, filter)
Expand Down Expand Up @@ -435,15 +410,17 @@ func rewriteAsGet(
return dgQuery
}

func rewriteAsQuery(field schema.Field, authRw *authRewriter) *gql.GraphQuery {
rbac := authRw.evaluateStaticRules(field.Type())
// Adds common RBAC and UID, Type rules to DQL query.
// This function is used by rewriteAsQuery and aggregateQuery functions
func addCommonRules(field schema.Field, fieldType schema.Type, authRw *authRewriter) (*gql.GraphQuery, schema.RuleResult) {
rbac := authRw.evaluateStaticRules(fieldType)
dgQuery := &gql.GraphQuery{
Attr: field.Name(),
}

if rbac == schema.Negative {
dgQuery.Attr = dgQuery.Attr + "()"
return dgQuery
return dgQuery, rbac
}

if authRw != nil && (authRw.isWritingAuth || authRw.filterByUid) && (authRw.varName != "" || authRw.parentVarName != "") {
Expand All @@ -460,10 +437,18 @@ func rewriteAsQuery(field schema.Field, authRw *authRewriter) *gql.GraphQuery {
authRw.varName = ""
authRw.filterByUid = false
}
} else if ids := idFilter(extractQueryFilter(field), field.Type().IDField()); ids != nil {
} else if ids := idFilter(extractQueryFilter(field), fieldType.IDField()); ids != nil {
addUIDFunc(dgQuery, ids)
} else {
addTypeFunc(dgQuery, field.Type().DgraphName())
addTypeFunc(dgQuery, fieldType.DgraphName())
}
return dgQuery, rbac
}

func rewriteAsQuery(field schema.Field, authRw *authRewriter) *gql.GraphQuery {
dgQuery, rbac := addCommonRules(field, field.Type(), authRw)
if rbac == schema.Negative {
return dgQuery
}

addArgumentsToField(dgQuery, field)
Expand Down

0 comments on commit 1b781e0

Please sign in to comment.