Skip to content

Commit

Permalink
Resolved review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shivaji-kharse committed Apr 17, 2023
1 parent aa02d7d commit e03168f
Show file tree
Hide file tree
Showing 6 changed files with 377 additions and 397 deletions.
142 changes: 32 additions & 110 deletions dgraphtest/acl_cluster.go → dgraphtest/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import (
"github.com/pkg/errors"
)

type AclGrpRules struct {
type AclGroupRules struct {
Predicate string `json:"predicate"`
Permission int32 `json:"permission"`
}
type AclGroup struct {
Name string `json:"name"`
Rules []AclGrpRules `json:"rules"`
Name string `json:"name"`
Rules []AclGroupRules `json:"rules"`
}

func (hc *HTTPClient) GetCurrentUser() (string, error) {
Expand All @@ -45,21 +45,19 @@ func (hc *HTTPClient) GetCurrentUser() (string, error) {
if err != nil {
return "", err
}
var currentUserResp struct {
var userResp struct {
GetCurrentUser struct {
Name string
}
}
if err := json.Unmarshal(resp, &currentUserResp); err != nil {
errors.Wrapf(err, "error unmarshalling getCurrentUser response %v")
if err := json.Unmarshal(resp, &userResp); err != nil {
return "", errors.Wrapf(err, "error unmarshalling getCurrentUser response %v")
}
return currentUserResp.GetCurrentUser.Name, nil
return userResp.GetCurrentUser.Name, nil
}

func (hc *HTTPClient) DeleteUser(username string) error {

delUser := `
mutation deleteUser($name: String!) {
delUser := `mutation deleteUser($name: String!) {
deleteUser(filter: {name: {eq: $name}}) {
msg
numUids
Expand All @@ -73,15 +71,11 @@ func (hc *HTTPClient) DeleteUser(username string) error {
},
}
_, err := hc.RunGraphqlQuery(params, true)
if err != nil {
return err
}
return nil
return err
}

func (hc *HTTPClient) CreateUser(username, password string) error {
addUser := `
mutation addUser($name: String!, $pass: String!) {
addUser := `mutation addUser($name: String!, $pass: String!) {
addUser(input: [{name: $name, password: $pass}]) {
user {
name
Expand All @@ -97,15 +91,11 @@ func (hc *HTTPClient) CreateUser(username, password string) error {
},
}
_, err := hc.RunGraphqlQuery(params, true)
if err != nil {
return err
}
return nil
return err
}

func (hc *HTTPClient) CreateGroup(name string) error {
addGroup := `
mutation addGroup($name: String!) {
addGroup := `mutation addGroup($name: String!) {
addGroup(input: [{name: $name}]) {
group {
name
Expand All @@ -120,24 +110,12 @@ func (hc *HTTPClient) CreateGroup(name string) error {
},
}
_, err := hc.RunGraphqlQuery(params, true)
if err != nil {
return err
}
return nil
return err
}

func (hc *HTTPClient) AddRulesToGroup(group string, rules []AclGrpRules) error {
func (hc *HTTPClient) AddRulesToGroup(group string, rules []AclGroupRules) error {
addRuleToGroup := `mutation updateGroup($name: String!, $rules: [RuleRef!]!) {
updateGroup(input: {
filter: {
name: {
eq: $name
}
},
set: {
rules: $rules
}
}) {
updateGroup(input: {filter: {name: {eq: $name}},set: {rules: $rules}}) {
group {
name
rules {
Expand All @@ -156,26 +134,12 @@ func (hc *HTTPClient) AddRulesToGroup(group string, rules []AclGrpRules) error {
},
}
_, err := hc.RunGraphqlQuery(params, true)
if err != nil {
return err
}
return nil
return err
}

func (hc *HTTPClient) AddToGroup(userName, group string) error {
func (hc *HTTPClient) AddUserToGroup(userName, group string) error {
addUserToGroup := `mutation updateUser($name: String!, $group: String!) {
updateUser(input: {
filter: {
name: {
eq: $name
}
},
set: {
groups: [
{ name: $group }
]
}
}) {
updateUser(input: {filter: {name: {eq: $name}},set: {groups: [{ name: $group }]}}) {
user {
name
groups {
Expand All @@ -193,24 +157,12 @@ func (hc *HTTPClient) AddToGroup(userName, group string) error {
},
}
_, err := hc.RunGraphqlQuery(params, true)
if err != nil {
return err
}
return nil
return err
}

func (hc *HTTPClient) RemoveUserFromGroup(userName, groupName string) error {
removeUserGroups := `mutation updateUser($name: String!, $groupName: String!) {
updateUser(input: {
filter: {
name: {
eq: $name
}
},
remove: {
groups: [{ name: $groupName }]
}
}) {
updateUser(input: {filter: {name: {eq: $name}},remove: {groups: [{ name: $groupName }]}}) {
user {
name
groups {
Expand All @@ -227,26 +179,13 @@ func (hc *HTTPClient) RemoveUserFromGroup(userName, groupName string) error {
"groupName": groupName,
},
}

_, err := hc.RunGraphqlQuery(params, true)
if err != nil {
return err
}
return nil
return err
}

func (hc *HTTPClient) RemoveRuleFromGroup(group string, rulePredicate string) error {
func (hc *HTTPClient) RemovePredicateFromGroup(group string, predicate string) error {
removeRuleFromGroup := `mutation updateGroup($name: String!, $rules: [String!]!) {
updateGroup(input: {
filter: {
name: {
eq: $name
}
},
remove: {
rules: $rules
}
}) {
updateGroup(input: {filter: {name: {eq: $name}},remove: {rules: $rules}}) {
group {
name
rules {
Expand All @@ -261,19 +200,15 @@ func (hc *HTTPClient) RemoveRuleFromGroup(group string, rulePredicate string) er
Query: removeRuleFromGroup,
Variables: map[string]interface{}{
"name": group,
"rules": []string{rulePredicate},
"rules": []string{predicate},
},
}
_, err := hc.RunGraphqlQuery(params, true)
if err != nil {
return err
}
return nil
return err
}

func (hc *HTTPClient) DeleteGroup(name string) error {
delGroup := `
mutation deleteGroup($name: String!) {
delGroup := `mutation deleteGroup($name: String!) {
deleteGroup(filter: {name: {eq: $name}}) {
msg
numUids
Expand All @@ -287,16 +222,12 @@ func (hc *HTTPClient) DeleteGroup(name string) error {
},
}
_, err := hc.RunGraphqlQuery(params, true)
if err != nil {
return err
}
return nil
return err
}

func (hc *HTTPClient) CreateGroupWithRules(name string, rules []AclGrpRules) (*AclGroup, error) {
func (hc *HTTPClient) CreateGroupWithRules(name string, rules []AclGroupRules) (*AclGroup, error) {
queryParams := GraphQLParams{
Query: `
mutation addGroup($name: String!, $rules: [RuleRef]){
Query: `mutation addGroup($name: String!, $rules: [RuleRef]){
addGroup(input: [
{
name: $name
Expand Down Expand Up @@ -337,20 +268,11 @@ func (hc *HTTPClient) CreateGroupWithRules(name string, rules []AclGrpRules) (*A
return &addGroupResp.AddGroup.Group[0], nil
}

func (hc *HTTPClient) UpdateGroup(name string, setRules []AclGrpRules,
func (hc *HTTPClient) UpdateGroup(name string, setRules []AclGroupRules,
removeRules []string) (*AclGroup, error) {
queryParams := GraphQLParams{
Query: `
mutation updateGroup($name: String!, $set: SetGroupPatch, $remove: RemoveGroupPatch){
updateGroup(input: {
filter: {
name: {
eq: $name
}
}
set: $set
remove: $remove
}) {
Query: `mutation updateGroup($name: String!, $set: SetGroupPatch, $remove: RemoveGroupPatch){
updateGroup(input: {filter: {name: {eq: $name}}set: $set remove: $remove}) {
group {
name
rules {
Expand Down
19 changes: 1 addition & 18 deletions dgraphtest/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"

"github.com/dgraph-io/dgo/v210"
"github.com/dgraph-io/dgo/v210/protos/api"
Expand Down Expand Up @@ -407,19 +406,12 @@ func (gc *GrpcClient) DropAll() error {
}

// Mutate performs a given mutation in a txn
func (gc *GrpcClient) Mutate(rdfs string, SetNQuads bool) (*api.Response, error) {
func (gc *GrpcClient) Mutate(mu *api.Mutation) (*api.Response, error) {
txn := gc.NewTxn()
defer func() { _ = txn.Discard(context.Background()) }()

ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
defer cancel()
var mu *api.Mutation
if SetNQuads {
mu = &api.Mutation{SetNquads: []byte(rdfs), CommitNow: true}
} else {
mu = &api.Mutation{DelNquads: []byte(rdfs), CommitNow: true}
}

return txn.Mutate(ctx, mu)
}

Expand Down Expand Up @@ -485,12 +477,3 @@ func isParent(ancestor, descendant string) (bool, error) {
}
return isParentCommit, nil
}

func (resp *GraphQLResponse) RequireNoGraphQLErrors(t *testing.T) {
if resp == nil {
require.Fail(t, "got nil response")
} else {
require.Nil(t, resp.Errors, "required no GraphQL errors, but received :\n%s",
resp.Errors.Error())
}
}
Loading

0 comments on commit e03168f

Please sign in to comment.