Skip to content

Commit

Permalink
Let result of checkpwd to be aliased (#2641)
Browse files Browse the repository at this point in the history
* Let result of checkpwd to be aliased

This changes query response to use any specified alias for response.
It also brings default checkpwd response to be in-line with count().
e.g.,

```
// before
"pass": [
  {
    "checkpwd": true
  }
]

// now
"checkpwd(pass)": true
```

Closes: #2630
  • Loading branch information
Gus authored Oct 9, 2018
1 parent 9a23cde commit a7c31a8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
9 changes: 5 additions & 4 deletions query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,15 +327,16 @@ func addInternalNode(pc *SubGraph, uid uint64, dst outputNode) error {
func addCheckPwd(pc *SubGraph, vals []*pb.TaskValue, dst outputNode) {
c := types.ValueForType(types.BoolID)
if len(vals) == 0 {
// No value found for predicate.
c.Value = false
} else {
c.Value = task.ToBool(vals[0])
}

uc := dst.New(pc.Attr)
uc.AddValue("checkpwd", c)
dst.AddListChild(pc.Attr, uc)
fieldName := pc.Params.Alias
if fieldName == "" {
fieldName = fmt.Sprintf("checkpwd(%s)", pc.Attr)
}
dst.AddValue(fieldName, c)
}

func alreadySeen(parentIds []uint64, uid uint64) bool {
Expand Down
54 changes: 40 additions & 14 deletions query/query3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,7 @@ func TestPasswordExpandAll2(t *testing.T) {
}
`
js := processToFastJsonNoErr(t, query)
require.JSONEq(t, `{"data":{"me":[{"_xid_":"mich","address":"31, 32 street, Jupiter","path":[{"path|weight":0.200000},{"path|weight":0.100000,"path|weight1":0.200000}],"sword_present":"true","dob_day":"1910-01-01T00:00:00Z","gender":"female","dob":"1910-01-01T00:00:00Z","survival_rate":98.990000,"noindex_name":"Michonne's name not indexed","name":"Michonne","graduation":["1932-01-01T00:00:00Z"],"bin_data":"YmluLWRhdGE=","loc":{"type":"Point","coordinates":[1.1,2]},"age":38,"full_name":"Michonne's large name for hashing","alive":true,"power":13.250000,"password":[{"checkpwd":false}]}]}}`, js)
require.JSONEq(t, `{"data":{"me":[{"_xid_":"mich","address":"31, 32 street, Jupiter","path":[{"path|weight":0.200000},{"path|weight":0.100000,"path|weight1":0.200000}],"sword_present":"true","dob_day":"1910-01-01T00:00:00Z","gender":"female","dob":"1910-01-01T00:00:00Z","survival_rate":98.990000,"noindex_name":"Michonne's name not indexed","name":"Michonne","graduation":["1932-01-01T00:00:00Z"],"bin_data":"YmluLWRhdGE=","loc":{"type":"Point","coordinates":[1.1,2]},"age":38,"full_name":"Michonne's large name for hashing","alive":true,"power":13.250000,"checkpwd(password)":false}]}}`, js)
}

func TestPasswordExpandError(t *testing.T) {
Expand All @@ -1099,9 +1099,7 @@ func TestCheckPassword(t *testing.T) {
}
`
js := processToFastJsonNoErr(t, query)
require.JSONEq(t,
`{"data": {"me":[{"name":"Michonne","password":[{"checkpwd":true}]}]}}`,
js)
require.JSONEq(t, `{"data": {"me":[{"name":"Michonne","checkpwd(password)":true}]}}`, js)
}

func TestCheckPasswordIncorrect(t *testing.T) {
Expand All @@ -1114,9 +1112,7 @@ func TestCheckPasswordIncorrect(t *testing.T) {
}
`
js := processToFastJsonNoErr(t, query)
require.JSONEq(t,
`{"data": {"me":[{"name":"Michonne","password":[{"checkpwd":false}]}]}}`,
js)
require.JSONEq(t, `{"data": {"me":[{"name":"Michonne","checkpwd(password)":false}]}}`, js)
}

// ensure, that old and deprecated form is not allowed
Expand Down Expand Up @@ -1144,7 +1140,7 @@ func TestCheckPasswordDifferentAttr1(t *testing.T) {
}
`
js := processToFastJsonNoErr(t, query)
require.JSONEq(t, `{"data": {"me":[{"name":"Rick Grimes","pass":[{"checkpwd":true}]}]}}`, js)
require.JSONEq(t, `{"data": {"me":[{"name":"Rick Grimes","checkpwd(pass)":true}]}}`, js)
}

func TestCheckPasswordDifferentAttr2(t *testing.T) {
Expand All @@ -1158,7 +1154,7 @@ func TestCheckPasswordDifferentAttr2(t *testing.T) {
}
`
js := processToFastJsonNoErr(t, query)
require.JSONEq(t, `{"data": {"me":[{"name":"Rick Grimes","pass":[{"checkpwd":false}]}]}}`, js)
require.JSONEq(t, `{"data": {"me":[{"name":"Rick Grimes","checkpwd(pass)":false}]}}`, js)
}

func TestCheckPasswordInvalidAttr(t *testing.T) {
Expand All @@ -1173,7 +1169,7 @@ func TestCheckPasswordInvalidAttr(t *testing.T) {
`
js := processToFastJsonNoErr(t, query)
// for id:0x1 there is no pass attribute defined (there's only password attribute)
require.JSONEq(t, `{"data": {"me":[{"name":"Michonne","pass":[{"checkpwd":false}]}]}}`, js)
require.JSONEq(t, `{"data": {"me":[{"name":"Michonne","checkpwd(pass)":false}]}}`, js)
}

// test for old version of checkpwd with hardcoded attribute name
Expand All @@ -1187,8 +1183,8 @@ func TestCheckPasswordQuery1(t *testing.T) {
}
}
`
_, err := processToFastJson(t, query)
require.NoError(t, err)
js := processToFastJsonNoErr(t, query)
require.JSONEq(t, `{"data": {"me":[{"name":"Michonne"}]}}`, js)
}

// test for improved version of checkpwd with custom attribute name
Expand All @@ -1202,8 +1198,38 @@ func TestCheckPasswordQuery2(t *testing.T) {
}
}
`
_, err := processToFastJson(t, query)
require.NoError(t, err)
js := processToFastJsonNoErr(t, query)
require.JSONEq(t, `{"data": {"me":[{"name":"Rick Grimes"}]}}`, js)
}

// test for improved version of checkpwd with alias for unknown attribute
func TestCheckPasswordQuery3(t *testing.T) {

query := `
{
me(func: uid(23)) {
name
secret: checkpwd(pass, "123456")
}
}
`
js := processToFastJsonNoErr(t, query)
require.JSONEq(t, `{"data": {"me":[{"name":"Rick Grimes","secret":false}]}}`, js)
}

// test for improved version of checkpwd with alias for known attribute
func TestCheckPasswordQuery4(t *testing.T) {

query := `
{
me(func: uid(0x01)) {
name
secreto: checkpwd(password, "123456")
}
}
`
js := processToFastJsonNoErr(t, query)
require.JSONEq(t, `{"data": {"me":[{"name":"Michonne","secreto":true}]}}`, js)
}

func TestToSubgraphInvalidFnName(t *testing.T) {
Expand Down

0 comments on commit a7c31a8

Please sign in to comment.