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 12, 2023
1 parent 82da9e4 commit 79c4b16
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 181 deletions.
196 changes: 196 additions & 0 deletions dgraphtest/acl_cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/*
* Copyright 2023 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dgraphtest

import (
"encoding/json"

"github.com/pkg/errors"
)

type AclGrpRules struct {
Predicate string `json:"predicate"`
Permission int32 `json:"permission"`
}

func (hc *HTTPClient) GetCurrentUser() (string, error) {
const query = `
query {
getCurrentUser {
name
}
}`
params := GraphQLParams{
Query: query,
}
resp, err := hc.RunGraphqlQuery(params, true)
if err != nil {
return "", err
}
var currentUserResp struct {
GetCurrentUser struct {
Name string
}
}
if err := json.Unmarshal(resp, &currentUserResp); err != nil {
errors.Wrapf(err, "error unmarshalling getCurrentUser response %v")
}
return currentUserResp.GetCurrentUser.Name, nil
}

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

delUser := `
mutation deleteUser($name: String!) {
deleteUser(filter: {name: {eq: $name}}) {
msg
numUids
}
}`

params := GraphQLParams{
Query: delUser,
Variables: map[string]interface{}{
"name": username,
},
}
_, err := hc.RunGraphqlQuery(params, true)
if err != nil {
return err
}
return nil
}

func (hc *HTTPClient) CreateUser(username, password string) error {
addUser := `
mutation addUser($name: String!, $pass: String!) {
addUser(input: [{name: $name, password: $pass}]) {
user {
name
}
}
}`

params := GraphQLParams{
Query: addUser,
Variables: map[string]interface{}{
"name": username,
"pass": password,
},
}
_, err := hc.RunGraphqlQuery(params, true)
if err != nil {
return err
}
return nil
}

func (hc *HTTPClient) CreateGroup(name string) error {
addGroup := `
mutation addGroup($name: String!) {
addGroup(input: [{name: $name}]) {
group {
name
}
}
}`

params := GraphQLParams{
Query: addGroup,
Variables: map[string]interface{}{
"name": name,
},
}
_, err := hc.RunGraphqlQuery(params, true)
if err != nil {
return err
}
return nil
}

func (hc *HTTPClient) AddRulesToGroup(group string, rules []AclGrpRules) error {
addRuleToGroup := `mutation updateGroup($name: String!, $rules: [RuleRef!]!) {
updateGroup(input: {
filter: {
name: {
eq: $name
}
},
set: {
rules: $rules
}
}) {
group {
name
rules {
predicate
permission
}
}
}
}`

params := GraphQLParams{
Query: addRuleToGroup,
Variables: map[string]interface{}{
"name": group,
"rules": rules,
},
}
_, err := hc.RunGraphqlQuery(params, true)
if err != nil {
return err
}
return nil
}

func (hc *HTTPClient) AddToGroup(userName, group string) error {
addUserToGroup := `mutation updateUser($name: String!, $group: String!) {
updateUser(input: {
filter: {
name: {
eq: $name
}
},
set: {
groups: [
{ name: $group }
]
}
}) {
user {
name
groups {
name
}
}
}
}`

params := GraphQLParams{
Query: addUserToGroup,
Variables: map[string]interface{}{
"name": userName,
"group": group,
},
}
_, err := hc.RunGraphqlQuery(params, true)
if err != nil {
return err
}
return nil
}
10 changes: 8 additions & 2 deletions dgraphtest/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,13 +407,19 @@ func (gc *GrpcClient) DropAll() error {
}

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

ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
defer cancel()
mu := &api.Mutation{SetNquads: []byte(rdfs), CommitNow: true}
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
1 change: 1 addition & 0 deletions dgraphtest/dgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const (

DefaultUser = "groot"
DefaultPassword = "password"
GalaxyNamespace = 0

localVersion = "local"
waitDurBeforeRetry = time.Second
Expand Down
Loading

0 comments on commit 79c4b16

Please sign in to comment.