resources_test.go 4.22 KB
Newer Older
ale's avatar
ale committed
1 2 3 4 5 6 7 8
package backend

import (
	"testing"

	"github.com/go-test/deep"
	"gopkg.in/ldap.v2"

9
	as "git.autistici.org/ai3/accountserver"
ale's avatar
ale committed
10 11 12 13
)

func TestEmailResource_FromLDAP(t *testing.T) {
	entry := ldap.NewEntry(
ale's avatar
ale committed
14
		"mail=test@investici.org,uid=test@investici.org,ou=People,"+testBaseDN,
ale's avatar
ale committed
15
		map[string][]string{
ale's avatar
ale committed
16 17 18 19 20 21 22
			"objectClass":          []string{"top", "virtualMailUser"},
			"mail":                 []string{"test@investici.org"},
			"status":               []string{"active"},
			"host":                 []string{"host1"},
			"originalHost":         []string{"host1"},
			"mailAlternateAddress": []string{"test2@investici.org", "test3@investici.org"},
			"mailMessageStore":     []string{"test/store"},
ale's avatar
ale committed
23 24 25 26
		},
	)

	reg := newResourceRegistry()
ale's avatar
ale committed
27
	reg.register(as.ResourceTypeEmail, &emailResourceHandler{baseDN: testBaseDN})
ale's avatar
ale committed
28 29 30 31 32 33

	r, err := reg.FromLDAP(entry)
	if err != nil {
		t.Fatal("FromLDAP", err)
	}

34
	expected := &as.Resource{
ale's avatar
ale committed
35
		ID:            as.ResourceID("mail=test@investici.org,uid=test@investici.org,ou=People," + testBaseDN),
ale's avatar
ale committed
36
		Type:          "email",
ale's avatar
ale committed
37 38 39 40
		Name:          "test@investici.org",
		Status:        "active",
		Shard:         "host1",
		OriginalShard: "host1",
41
		Email: &as.Email{
ale's avatar
ale committed
42 43 44 45 46 47 48 49
			Aliases: []string{"test2@investici.org", "test3@investici.org"},
			Maildir: "test/store",
		},
	}
	if err := deep.Equal(r, expected); err != nil {
		t.Fatalf("bad result: %v", err)
	}
}
ale's avatar
ale committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

func TestResource_LDAPSerialization(t *testing.T) {
	var testResources = []*as.Resource{
		&as.Resource{
			ID:     as.ResourceID("mail=test@investici.org,uid=test@investici.org,ou=People," + testBaseDN),
			Type:   as.ResourceTypeEmail,
			Name:   "test@investici.org",
			Status: as.ResourceStatusActive,
			Email: &as.Email{
				Aliases: []string{"test2@investici.org", "test3@investici.org"},
				Maildir: "test/store",
			},
		},

		&as.Resource{
			ID:     as.ResourceID("cn=site.investici.org,uid=test@investici.org,ou=People," + testBaseDN),
			Type:   as.ResourceTypeDomain,
			Name:   "site.investici.org",
			Status: as.ResourceStatusActive,
			Website: &as.Website{
				URL:          "https://site.investici.org/",
				UID:          123456,
				AcceptMail:   true,
				Options:      []string{"php", "sendmail"},
				DocumentRoot: "/home/web/root",
			},
		},

		&as.Resource{
			ID:     as.ResourceID("alias=site,uid=test@investici.org,ou=People," + testBaseDN),
			Type:   as.ResourceTypeWebsite,
			Name:   "site",
			Status: as.ResourceStatusActive,
			Website: &as.Website{
				URL:          "https://www.investici.org/site/",
				UID:          123456,
				ParentDomain: "investici.org",
				AcceptMail:   true,
				Options:      []string{"php", "sendmail"},
				DocumentRoot: "/home/web/root",
			},
		},

		&as.Resource{
			ID:     as.ResourceID("ftpname=test,uid=test@investici.org,ou=People," + testBaseDN),
			Type:   as.ResourceTypeDAV,
			Name:   "test",
			Status: as.ResourceStatusActive,
			DAV: &as.WebDAV{
				UID:     123456,
				Homedir: "/home/web/test",
			},
		},

		&as.Resource{
			ID:       as.ResourceID("dbname=testdb,alias=site,uid=test@investici.org,ou=People," + testBaseDN),
			ParentID: as.ResourceID("alias=site,uid=test@investici.org,ou=People," + testBaseDN),
			Type:     as.ResourceTypeDatabase,
			Name:     "testdb",
			Status:   as.ResourceStatusActive,
			Database: &as.Database{
				DBUser:            "testdbuser",
				CleartextPassword: "secretpassword",
			},
		},

		&as.Resource{
			ID:     as.ResourceID("listName=test@investici.org,ou=Lists," + testBaseDN),
			Type:   as.ResourceTypeMailingList,
			Name:   "test@investici.org",
			Status: as.ResourceStatusActive,
			List: &as.MailingList{
				Admins: []string{"admin@investici.org"},
				Public: true,
			},
		},
	}

	reg := newDefaultResourceRegistry(testBaseDN)

	for _, r := range testResources {
		attrs := reg.ToLDAP(r)
		e := partialAttrsToEntry(r.ID.String(), attrs)
		r2, err := reg.FromLDAP(e)
		if err != nil {
			t.Errorf("error deserializing resource %s: %v", r.String(), err)
			continue
		}
		if diff := deep.Equal(r, r2); diff != nil {
			t.Errorf("differences for resource %s: %v", r.String(), diff)
		}
	}
}

func partialAttrsToEntry(dn string, attrs []ldap.PartialAttribute) *ldap.Entry {
	amap := make(map[string][]string)
	for _, p := range attrs {
		amap[p.Type] = p.Vals
	}
	return ldap.NewEntry(dn, amap)
}