Skip to content
Snippets Groups Projects
Commit 66a3e2d6 authored by ale's avatar ale
Browse files

Fix availability checks for some missed list/newsletter cases

Add tests for that too.
parent a319c94b
Branches
No related tags found
1 merge request!42Support Webauthn
package accountserver
import (
"errors"
"fmt"
"log"
)
......@@ -196,7 +195,7 @@ func (r *CheckResourceAvailabilityRequest) Validate(rctx *RequestContext) error
func (r *CheckResourceAvailabilityRequest) Serve(rctx *RequestContext) (interface{}, error) {
var check ValidatorFunc
switch r.Type {
case ResourceTypeEmail, ResourceTypeMailingList:
case ResourceTypeEmail, ResourceTypeMailingList, ResourceTypeNewsletter:
check = rctx.validationCtx.isAvailableEmailAddr()
case ResourceTypeDomain:
check = rctx.validationCtx.isAvailableDomain()
......@@ -207,7 +206,7 @@ func (r *CheckResourceAvailabilityRequest) Serve(rctx *RequestContext) (interfac
case ResourceTypeDatabase:
check = rctx.validationCtx.isAvailableDatabase()
default:
return nil, errors.New("unknown resource type")
return nil, newValidationError(nil, "type", "unknown resource type")
}
var resp CheckResourceAvailabilityResponse
......
......@@ -100,7 +100,8 @@ func setCommonResourceAttrs(entry *ldap.Entry, rsrc *as.Resource) {
func (reg *resourceRegistry) FromLDAP(entry *ldap.Entry) (rsrc *as.Resource, err error) {
// Since we don't know what resource type to expect, we try
// all known handlers until one returns a valid Resource.
// This is slightly dangerous unless all
// This expects that all object types can be told apart by
// their DN or attributes.
for _, h := range reg.handlers {
rsrc, err = h.FromLDAP(entry)
if err == nil {
......
package integrationtest
import (
"testing"
as "git.autistici.org/ai3/accountserver"
)
func TestIntegration_CheckResourceAvailability(t *testing.T) {
stop, _, c := startService(t)
defer stop()
testdata := []struct {
rtype string
name string
expectedAvail bool
}{
{"email", "uno@investici.org", false},
{"email", "due@investici.org", false},
{"email", "tre@investici.org", false},
{"email", "newsletter1@investici.org", false},
{"email", "list4@investici.org", false},
{"email", "new@investici.org", true},
{"list", "newsletter1@investici.org", false},
{"list", "list4@investici.org", false},
{"newsletter", "newsletter1@investici.org", false},
{"newsletter", "list4@investici.org", false},
{"web", "due", false},
{"web", "new", true},
{"dav", "uno", false},
{"dav", "new", true},
}
for _, td := range testdata {
var resp as.CheckResourceAvailabilityResponse
err := c.request("/api/resource/check_availability", &as.CheckResourceAvailabilityRequest{
Type: td.rtype,
Name: td.name,
}, &resp)
if err != nil {
t.Errorf("CheckResourceAvailability(%s/%s) error: %v", td.rtype, td.name, err)
continue
}
if resp.Available != td.expectedAvail {
t.Errorf("CheckResourceAvailability(%s/%s) returned %v, expected %v", td.rtype, td.name, resp.Available, td.expectedAvail)
}
}
}
......@@ -41,3 +41,14 @@ status: active
uidNumber: 23801
userPassword:: JGEyJDQkMzI3NjgkMSQwZDgyMzU1YjQ0Mzg0M2NmZDY4MjU1MzE4ZTVjYTdiZSRmNTQ0ODkxOTFiNWZlYzk2MDRlNWQ2ODZjMDQxZjJkNTFmOTgxOGY4ZTFmM2E4MDYzY2U3ZTEwMTE3OTc2OGI0
dn: listName=list4@investici.org,ou=Lists,dc=example,dc=com
objectClass: mailingList
objectClass: top
listName: list4@investici.org
listDescription: Mailing list 4
public: no
status: active
listOwner: quattro@investici.org
host: host2
originalHost: host2
......@@ -251,6 +251,8 @@ func errToStatus(err error) int {
return http.StatusForbidden
case as.IsRequestError(err):
return http.StatusBadRequest
case as.IsValidationError(err):
return http.StatusBadRequest
default:
return http.StatusInternalServerError
}
......
......@@ -349,14 +349,18 @@ func splitEmailAddr(addr string) (string, string) {
// Returns all the possible resources in the email and mailing list
// namespaces that might conflict with the given email address.
func relatedEmails(ctx context.Context, be domainBackend, addr string) []FindResourceRequest {
// Check for the literal addr in the unified mail+list namespace.
rel := []FindResourceRequest{
{Type: ResourceTypeEmail, Name: addr},
{Type: ResourceTypeMailingList, Name: addr},
{Type: ResourceTypeNewsletter, Name: addr},
}
user, _ := splitEmailAddr(addr)
// Mailing lists and newsletters must have unique names
// regardless of the domain, so we add potential conflicts for
// mailing lists with the same name over all list-enabled
// domains.
user, _ := splitEmailAddr(addr)
for _, d := range be.GetAllowedDomains(ctx, ResourceTypeMailingList) {
rel = append(rel, FindResourceRequest{
Type: ResourceTypeMailingList,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment