Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions v2/account_claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,9 @@ type WeightedMapping struct {
Cluster string `json:"cluster,omitempty"`
}

// GetWeight returns the weight value.
// Deprecated: use Weight field directly.
func (m *WeightedMapping) GetWeight() uint8 {
if m.Weight == 0 {
return 100
}
return m.Weight
}

Expand All @@ -164,7 +163,7 @@ func (m *Mapping) Validate(vr *ValidationResults) {
vr.AddError("Mapping %q in cluster %q exceeds 100%% among all of it's weighted to mappings", ubFrom, e.Cluster)
}
} else {
total += e.GetWeight()
total += e.Weight
}
}
if total > 100 {
Expand Down
41 changes: 40 additions & 1 deletion v2/account_claims_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ func TestAccountMapping(t *testing.T) { // don't block encoding!!!
vr = &ValidationResults{}
account.Mappings = Mapping{}
account.AddMapping("foo4",
WeightedMapping{Subject: "to1"}, // no weight means 100
WeightedMapping{Subject: "to1", Weight: 100},
WeightedMapping{Subject: "to2", Weight: 1})
account.Validate(vr)
if !vr.IsBlocking(false) {
Expand Down Expand Up @@ -743,6 +743,45 @@ func TestAccountClusterNoOver100Mapping(t *testing.T) { // don't block encoding!
}
}

func TestAccountClusterMappingWithZeroWeights(t *testing.T) {
akp := createAccountNKey(t)
apk := publicKey(akp, t)

account := NewAccountClaims(apk)
vr := &ValidationResults{}

// multiple mappings with weight 0 should be allowed and not count toward total
account.AddMapping("q",
WeightedMapping{Subject: "qq", Weight: 0, Cluster: "A"}, // 0 weight in cluster A
WeightedMapping{Subject: "bb", Weight: 100, Cluster: "A"}, // 100 weight in cluster A (total: 100)
WeightedMapping{Subject: "cc", Weight: 0, Cluster: "B"}, // 0 weight in cluster B
WeightedMapping{Subject: "dd", Weight: 0, Cluster: "B"}, // another 0 weight in cluster B
WeightedMapping{Subject: "ee", Weight: 50, Cluster: "B"}, // 50 weight in cluster B (total: 50)
WeightedMapping{Subject: "ff", Weight: 0}, // 0 weight non-cluster
WeightedMapping{Subject: "gg", Weight: 50}) // 50 weight non-cluster (total: 50)
account.Validate(vr)
if !vr.IsEmpty() {
t.Fatal("Expected no errors")
}
}

func TestAccountMappingWith30And0Weights(t *testing.T) {
akp := createAccountNKey(t)
apk := publicKey(akp, t)

account := NewAccountClaims(apk)
vr := &ValidationResults{}

// weight 30 + weight 0 = 30, should be valid
account.AddMapping("q",
WeightedMapping{Subject: "qq", Weight: 30},
WeightedMapping{Subject: "bb", Weight: 0})
account.Validate(vr)
if !vr.IsEmpty() {
t.Fatal("Expected no errors")
}
}

func TestAccountExternalAuthorization(t *testing.T) {
akp := createAccountNKey(t)
apk := publicKey(akp, t)
Expand Down
Loading