-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Auth rules were not applied on type having @dgraph directive because they were stored in authRules map corresponding to Graphql type name and was fetched with Dgraph type name when rewriting the query.
- Loading branch information
Arijit Das
authored
Jun 24, 2020
1 parent
cce4a37
commit 8562311
Showing
4 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,6 +113,11 @@ type Project struct { | |
Columns []*Column `json:"columns,omitempty"` | ||
} | ||
|
||
type Student struct { | ||
Id string `json:"id,omitempty"` | ||
Email string `json:"email,omitempty"` | ||
} | ||
|
||
type TestCase struct { | ||
user string | ||
role string | ||
|
@@ -147,6 +152,87 @@ func getJWT(t *testing.T, user, role string) http.Header { | |
return h | ||
} | ||
|
||
func (s Student) deleteByEmail(t *testing.T) { | ||
getParams := &common.GraphQLParams{ | ||
Query: ` | ||
mutation delStudent ($filter : StudentFilter!){ | ||
deleteStudent (filter: $filter) { | ||
numUids | ||
} | ||
} | ||
`, | ||
Variables: map[string]interface{}{"filter": map[string]interface{}{ | ||
"email": map[string]interface{}{"eq": s.Email}, | ||
}}, | ||
} | ||
gqlResponse := getParams.ExecuteAsPost(t, graphqlURL) | ||
require.Nil(t, gqlResponse.Errors) | ||
} | ||
|
||
func (s Student) add(t *testing.T) { | ||
mutation := &common.GraphQLParams{ | ||
Query: ` | ||
mutation addStudent($student : AddStudentInput!) { | ||
addStudent(input: [$student]) { | ||
numUids | ||
} | ||
}`, | ||
Variables: map[string]interface{}{"student": s}, | ||
} | ||
result := `{"addStudent":{"numUids": 1}}` | ||
gqlResponse := mutation.ExecuteAsPost(t, graphqlURL) | ||
common.RequireNoGQLErrors(t, gqlResponse) | ||
require.JSONEq(t, result, string(gqlResponse.Data)) | ||
} | ||
|
||
func TestAuthWithDgraphDirective(t *testing.T) { | ||
students := []Student{ | ||
{ | ||
Email: "[email protected]", | ||
}, | ||
{ | ||
Email: "[email protected]", | ||
}, | ||
} | ||
for _, student := range students { | ||
student.add(t) | ||
} | ||
|
||
testCases := []TestCase{{ | ||
user: students[0].Email, | ||
role: "ADMIN", | ||
result: `{"queryStudent":[{"email":"` + students[0].Email + `"}]}`, | ||
}, { | ||
user: students[0].Email, | ||
role: "USER", | ||
result: `{"queryStudent" : []}`, | ||
}} | ||
|
||
queryStudent := ` | ||
query { | ||
queryStudent { | ||
} | ||
}` | ||
|
||
for _, tcase := range testCases { | ||
t.Run(tcase.role+"_"+tcase.user, func(t *testing.T) { | ||
queryParams := &common.GraphQLParams{ | ||
Query: queryStudent, | ||
Headers: getJWT(t, tcase.user, tcase.role), | ||
} | ||
gqlResponse := queryParams.ExecuteAsPost(t, graphqlURL) | ||
common.RequireNoGQLErrors(t, gqlResponse) | ||
require.JSONEq(t, tcase.result, string(gqlResponse.Data)) | ||
}) | ||
} | ||
|
||
// Clean up | ||
for _, student := range students { | ||
student.deleteByEmail(t) | ||
} | ||
} | ||
|
||
func TestAuthRulesWithMissingJWT(t *testing.T) { | ||
testCases := []TestCase{ | ||
{name: "Query non auth field without JWT Token", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters