added doormen ownership

This commit is contained in:
didier amyot committed 2015-06-14 23:21:42 -04:00
1 parent f0628e4682
commit f7a8d416c1
27 files changed
+489 -126

No files matched your search

+22 -8
View File
@@ -31,23 +31,24 @@ func (wId *DoormanId) AsJson() string {
}
type DoormanDefinition struct {
Id bson.ObjectId `json:"id,omitempty" bson:"_id"`
Name string `json:"name" bson:"name"`
Values []*DoormanValue `json:"values" bson:"values"`
Id bson.ObjectId `json:"id,omitempty" bson:"_id"`
Name string `json:"name" bson:"name"`
Values []*DoormanValue `json:"values" bson:"values"`
OwnerEmails []string `json:"emails" bson:"emails"`
}
func NewDoormanDefinition(name string) *DoormanDefinition {
return &DoormanDefinition{Id: bson.NewObjectId(), Name: name}
func NewDoormanDefinition(name string, ownerEmails ...string) *DoormanDefinition {
return &DoormanDefinition{Id: bson.NewObjectId(), Name: name, OwnerEmails: ownerEmails}
}
// a way to quickly create doormen in tests. do not use in real code. will
// panic if a doorman is wrongly specify
func QuickNewDoormanDefinition(name string, probs ...float64) *DoormanDefinition {
ret := NewDoormanDefinition(name)
ret := NewDoormanDefinition(name, "natasha@bigtits.com")
for i, prob := range probs {
ret.Values = append(ret.Values, &DoormanValue{"T" + strconv.Itoa(i), prob})
}
if err := ret.Validate(); err != nil {
if err := ret.Validate("natasha@bigtits.com"); err != nil {
panic(err)
}
return ret
@@ -68,6 +69,15 @@ func (dmd *DoormanDefinition) ValidateDoormanValueNames() error {
return nil
}
func (dmd *DoormanDefinition) AsWriteAccess(email string) bool {
for _, email_ := range dmd.OwnerEmails {
if email_ == email {
return true
}
}
return false
}
func (dmd *DoormanDefinition) ValidateDoormanValueProbabilities() error {
var sum float64 = 0
for _, w := range dmd.Values {
@@ -84,7 +94,7 @@ func (dmd *DoormanDefinition) ValidateDoormanValueProbabilities() error {
return nil
}
func (dmd *DoormanDefinition) Validate() (err error) {
func (dmd *DoormanDefinition) Validate(author string) (err error) {
switch {
case dmd.Name == "":
err = errors.New("name cannot be empty")
@@ -94,6 +104,10 @@ func (dmd *DoormanDefinition) Validate() (err error) {
err = dmd.ValidateDoormanValueNames()
case dmd.ValidateDoormanValueProbabilities() != nil:
err = dmd.ValidateDoormanValueProbabilities()
case len(dmd.OwnerEmails) < 1:
err = errors.New("A doorman needs at least one owner specify as 'email'.")
case !dmd.AsWriteAccess(author):
err = errors.New(fmt.Sprintf("'%s' is not allow to edit this doorman.", author))
}
return
}
+34 -9
View File
@@ -21,45 +21,53 @@ func TestNewDoormanDefinition(t *testing.T) {
func TestValidate(t *testing.T) {
dmd := &DoormanDefinition{Name: ""}
if err := dmd.Validate(); err.Error() != "name cannot be empty" {
if err := dmd.Validate("bob"); err.Error() != "name cannot be empty" {
t.Error(err)
}
dmd = &DoormanDefinition{Name: "foo"}
if err := dmd.Validate(); err.Error() != "need at least 2 values" {
if err := dmd.Validate("bob"); err.Error() != "need at least 2 values" {
t.Error(err)
}
dmd = &DoormanDefinition{Name: "foo", Values: []*DoormanValue{nil}}
if err := dmd.Validate(); err.Error() != "need at least 2 values" {
if err := dmd.Validate("bob"); err.Error() != "need at least 2 values" {
t.Error(err)
}
dmd = &DoormanDefinition{Name: "foo", Values: []*DoormanValue{&DoormanValue{"foo", 0}, &DoormanValue{"foo", 0}}}
if err := dmd.Validate(); err.Error() != "doorman value names must be unique within a doorman" {
if err := dmd.Validate("bob"); err.Error() != "doorman value names must be unique within a doorman" {
t.Error(err)
}
dmd = &DoormanDefinition{Name: "foo", Values: []*DoormanValue{&DoormanValue{"foo", 0}, &DoormanValue{"", 0}}}
if err := dmd.Validate(); err.Error() != "doorman value cannot be empty" {
if err := dmd.Validate("bob"); err.Error() != "doorman value cannot be empty" {
t.Error(err)
}
dmd = &DoormanDefinition{Name: "foo", Values: []*DoormanValue{&DoormanValue{"foo", -0.25}, &DoormanValue{"bar", 0.5}}}
if err := dmd.Validate(); err.Error() != "doorman value -0.25 is out of range" {
if err := dmd.Validate("bob"); err.Error() != "doorman value -0.25 is out of range" {
t.Error(err)
}
dmd = &DoormanDefinition{Name: "foo", Values: []*DoormanValue{&DoormanValue{"foo", 2.5}, &DoormanValue{"bar", 0.5}}}
if err := dmd.Validate(); err.Error() != "doorman value 2.5 is out of range" {
if err := dmd.Validate("bob"); err.Error() != "doorman value 2.5 is out of range" {
t.Error(err)
}
dmd = &DoormanDefinition{Name: "foo", Values: []*DoormanValue{&DoormanValue{"foo", 0.75}, &DoormanValue{"bar", 0.5}}}
if err := dmd.Validate(); err.Error() != "the sum of the probability must be 1.0" {
if err := dmd.Validate("bob"); err.Error() != "the sum of the probability must be 1.0" {
t.Error(err)
}
dmd = &DoormanDefinition{Name: "foo", Values: []*DoormanValue{&DoormanValue{"foo", 0.2}, &DoormanValue{"bar", 0.2}}}
if err := dmd.Validate(); err.Error() != "the sum of the probability must be 1.0" {
if err := dmd.Validate("bob"); err.Error() != "the sum of the probability must be 1.0" {
t.Error(err)
}
dmd = &DoormanDefinition{Name: "foo", Values: []*DoormanValue{&DoormanValue{"foo", 0.8}, &DoormanValue{"bar", 0.2}}}
if err := dmd.Validate("bob"); err.Error() != "A doorman needs at least one owner specify as 'email'." {
t.Error(err)
}
dmd = &DoormanDefinition{Name: "foo", Values: []*DoormanValue{&DoormanValue{"foo", 0.8}, &DoormanValue{"bar", 0.2}}, OwnerEmails: []string{"alice"}}
if err := dmd.Validate("bob"); err.Error() != "'bob' is not allow to edit this doorman." {
t.Error(err)
}
}
@@ -126,4 +134,21 @@ func TestQuickNewDoormanDefinition(t *testing.T) {
if dmd.Values[1].Name != "T1" || dmd.Values[1].Probability != 0.25 {
t.Error()
}
if len(dmd.OwnerEmails) != 1 && dmd.OwnerEmails[0] != "natasha@bigtits.com" {
t.Error()
}
}
func TestAsWriteAccess(t *testing.T) {
dmd := &DoormanDefinition{OwnerEmails: []string{"natasha@bigtits.com"}}
testCases := map[string]bool{
"natasha@bigtits.com": true,
"natasha@bigtis.net": false,
}
for email, hasAccess := range testCases {
if hasAccess != dmd.AsWriteAccess(email) {
t.Error(email)
}
}
}