github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/policy/localcopy/repository_test.go (about)

     1  /*
     2   * Copyright (C) 2020 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package localcopy
    19  
    20  import (
    21  	"testing"
    22  
    23  	"github.com/mysteriumnetwork/node/identity"
    24  	"github.com/mysteriumnetwork/node/market"
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  var (
    29  	policyOne      = market.AccessPolicy{ID: "1", Source: "http://policy.localhost/1"}
    30  	policyOneRules = market.AccessPolicyRuleSet{
    31  		ID:    "1",
    32  		Title: "One",
    33  		Allow: []market.AccessRule{
    34  			{Type: market.AccessPolicyTypeIdentity, Value: "0x1"},
    35  		},
    36  	}
    37  	policyOneRulesUpdated = market.AccessPolicyRuleSet{
    38  		ID:    "1",
    39  		Title: "One (updated)",
    40  		Allow: []market.AccessRule{
    41  			{Type: market.AccessPolicyTypeIdentity, Value: "0x1"},
    42  		},
    43  	}
    44  
    45  	policyTwo      = market.AccessPolicy{ID: "2", Source: "http://policy.localhost/2"}
    46  	policyTwoRules = market.AccessPolicyRuleSet{
    47  		ID:    "2",
    48  		Title: "Two",
    49  		Allow: []market.AccessRule{
    50  			{Type: market.AccessPolicyTypeDNSHostname, Value: "ipinfo.io"},
    51  		},
    52  	}
    53  	policyTwoRulesUpdated = market.AccessPolicyRuleSet{
    54  		ID:    "2",
    55  		Title: "Two (updated)",
    56  		Allow: []market.AccessRule{
    57  			{Type: market.AccessPolicyTypeDNSHostname, Value: "ipinfo.io"},
    58  		},
    59  	}
    60  
    61  	policyThree             = market.AccessPolicy{ID: "3", Source: "http://policy.localhost/3"}
    62  	policyThreeRulesUpdated = market.AccessPolicyRuleSet{
    63  		ID:    "3",
    64  		Title: "Three (updated)",
    65  		Allow: []market.AccessRule{
    66  			{Type: market.AccessPolicyTypeDNSZone, Value: "ipinfo.io"},
    67  		},
    68  	}
    69  )
    70  
    71  func Test_Repository_RulesForPolicy(t *testing.T) {
    72  	repo := createEmptyRepo()
    73  	policyRules, err := repo.RulesForPolicy(policyOne)
    74  	assert.EqualError(t, err, "unknown policy: {1 http://policy.localhost/1}")
    75  	assert.Equal(t, market.AccessPolicyRuleSet{}, policyRules)
    76  
    77  	repo = createFullRepo()
    78  	policyRules, err = repo.RulesForPolicy(policyOne)
    79  	assert.NoError(t, err)
    80  	assert.Equal(t, policyOneRules, policyRules)
    81  }
    82  
    83  func Test_Repository_RulesForPolicies(t *testing.T) {
    84  	repo := createEmptyRepo()
    85  	policiesRules, err := repo.RulesForPolicies([]market.AccessPolicy{
    86  		policyOne,
    87  		policyThree,
    88  	})
    89  	assert.EqualError(t, err, "unknown policy: {1 http://policy.localhost/1}")
    90  	assert.Equal(t, []market.AccessPolicyRuleSet{}, policiesRules)
    91  
    92  	repo = createFullRepo()
    93  	policiesRules, err = repo.RulesForPolicies([]market.AccessPolicy{
    94  		policyOne,
    95  		policyTwo,
    96  	})
    97  	assert.NoError(t, err)
    98  	assert.Equal(t, []market.AccessPolicyRuleSet{policyOneRules, policyTwoRules}, policiesRules)
    99  }
   100  
   101  func Test_Repository_SetPolicyRules(t *testing.T) {
   102  	repo := NewRepository()
   103  	repo.SetPolicyRules(
   104  		policyOne,
   105  		market.AccessPolicyRuleSet{
   106  			ID:    "1",
   107  			Title: "One",
   108  			Allow: []market.AccessRule{
   109  				{Type: market.AccessPolicyTypeIdentity, Value: "0x1"},
   110  			},
   111  		},
   112  	)
   113  
   114  	repo.SetPolicyRules(
   115  		policyOne,
   116  		market.AccessPolicyRuleSet{
   117  			ID:    "1",
   118  			Title: "One",
   119  			Allow: []market.AccessRule{
   120  				{Type: market.AccessPolicyTypeIdentity, Value: "0x2"},
   121  			},
   122  		},
   123  	)
   124  
   125  	assert.True(t, repo.IsIdentityAllowed(identity.FromAddress("0x2")))
   126  }
   127  
   128  func Test_Repository_Rules(t *testing.T) {
   129  	repo := createEmptyRepo()
   130  	assert.Equal(t, []market.AccessPolicyRuleSet{}, repo.Rules())
   131  
   132  	repo = createFullRepo()
   133  	assert.Equal(t, []market.AccessPolicyRuleSet{policyOneRules, policyTwoRules}, repo.Rules())
   134  }
   135  
   136  func createEmptyRepo() *Repository {
   137  	return NewRepository()
   138  }
   139  
   140  func createFullRepo() *Repository {
   141  	repo := NewRepository()
   142  	repo.SetPolicyRules(
   143  		policyOne,
   144  		market.AccessPolicyRuleSet{
   145  			ID:    "1",
   146  			Title: "One",
   147  			Allow: []market.AccessRule{
   148  				{Type: market.AccessPolicyTypeIdentity, Value: "0x1"},
   149  			},
   150  		},
   151  	)
   152  	repo.SetPolicyRules(
   153  		policyTwo,
   154  		market.AccessPolicyRuleSet{
   155  			ID:    "2",
   156  			Title: "Two",
   157  			Allow: []market.AccessRule{
   158  				{Type: market.AccessPolicyTypeDNSHostname, Value: "ipinfo.io"},
   159  			},
   160  		},
   161  	)
   162  	return repo
   163  }