github.com/unigraph-dev/dgraph@v1.1.1-0.20200923154953-8b52b426f765/ee/acl/groups_test.go (about)

     1  // +build !oss
     2  
     3  /*
     4   * Copyright 2018 Dgraph Labs, Inc. and Contributors
     5   *
     6   * Licensed under the Dgraph Community License (the "License"); you
     7   * may not use this file except in compliance with the License. You
     8   * may obtain a copy of the License at
     9   *
    10   *     https://github.com/dgraph-io/dgraph/blob/master/licenses/DCL.txt
    11   */
    12  package acl
    13  
    14  import (
    15  	"testing"
    16  
    17  	"github.com/stretchr/testify/require"
    18  )
    19  
    20  func TestUpdateAcl(t *testing.T) {
    21  	var currenAcls []Acl
    22  	newAcl := Acl{
    23  		Predicate: "friend",
    24  		Perm:      4,
    25  	}
    26  
    27  	updatedAcls1, changed := updateAcl(currenAcls, newAcl)
    28  	require.True(t, changed, "the acl list should be changed")
    29  	require.Equal(t, 1, len(updatedAcls1),
    30  		"the updated acl list should have 1 element")
    31  
    32  	// trying to update the acl list again with the exactly same acl won't change it
    33  	updatedAcls2, changed := updateAcl(updatedAcls1, newAcl)
    34  	require.False(t, changed,
    35  		"the acl list should not be changed through update with an existing element")
    36  	require.Equal(t, 1, len(updatedAcls2),
    37  		"the updated acl list should still have 1 element")
    38  	require.Equal(t, int32(4), updatedAcls2[0].Perm,
    39  		"the perm should still have the value of 4")
    40  
    41  	newAcl.Perm = 6
    42  	updatedAcls3, changed := updateAcl(updatedAcls1, newAcl)
    43  	require.True(t, changed, "the acl list should be changed through update "+
    44  		"with element of new perm")
    45  	require.Equal(t, 1, len(updatedAcls3),
    46  		"the updated acl list should still have 1 element")
    47  	require.Equal(t, int32(6), updatedAcls3[0].Perm,
    48  		"the updated perm should be 6 now")
    49  
    50  	newAcl = Acl{
    51  		Predicate: "buddy",
    52  		Perm:      6,
    53  	}
    54  
    55  	updatedAcls4, changed := updateAcl(updatedAcls3, newAcl)
    56  	require.True(t, changed, "the acl should be changed through update "+
    57  		"with element of new predicate")
    58  	require.Equal(t, 2, len(updatedAcls4),
    59  		"the acl list should have 2 elements now")
    60  
    61  	newAcl = Acl{
    62  		Predicate: "buddy",
    63  		Perm:      -3,
    64  	}
    65  
    66  	updatedAcls5, changed := updateAcl(updatedAcls4, newAcl)
    67  	require.True(t, changed, "the acl should be changed through update "+
    68  		"with element of negative predicate")
    69  	require.Equal(t, 1, len(updatedAcls5),
    70  		"the acl list should have 1 element now")
    71  	require.Equal(t, "friend", updatedAcls5[0].Predicate,
    72  		"the left acl should have the original first predicate")
    73  }