Skip to content

Commit

Permalink
Split createlogin into createAccount and createSubscriber
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Archibald committed Jan 31, 2017
1 parent 2e044a5 commit 3ca9112
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 20 deletions.
2 changes: 1 addition & 1 deletion authStore.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ func (s *authStore) createLogin(userID, dbUserID int, email, fullName, password
homeDirectory := "/home"
mQuota := fmt.Sprintf("%dGB", mailQuota)
fQuota := fmt.Sprintf("%dGB", fileQuota)
login, err := s.backend.CreateLogin(userID, dbUserID, email, passwordHash, fullName, homeDirectory, uidNumber, gidNumber, mQuota, fQuota)
login, err := s.backend.CreateSubscriber(userID, dbUserID, email, passwordHash, fullName, homeDirectory, uidNumber, gidNumber, mQuota, fQuota)
if err != nil {
return nil, newLoggedError("Unable to create login", err)
}
Expand Down
6 changes: 3 additions & 3 deletions authStore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ var createProfileTests = []struct {
EmailCookie: &emailCookie{EmailVerificationCode: "nfwRDzfxxJj2_HY-_mLz6jWyWU7bF0zUlIUUVkQgbZ0=", ExpireTimeUTC: time.Now()},
getEmailSessionReturn: getEmailSessionSuccess(),
LoginReturn: loginErr(),
MethodsCalled: []string{"GetEmailSession", "UpdateUser", "DeleteEmailSession", "CreateLogin"},
MethodsCalled: []string{"GetEmailSession", "UpdateUser", "DeleteEmailSession", "CreateSubscriber"},
ExpectedErr: "Unable to create login",
},
{
Expand All @@ -456,7 +456,7 @@ var createProfileTests = []struct {
getEmailSessionReturn: getEmailSessionSuccess(),
LoginReturn: loginSuccess(),
CreateSessionReturn: sessionRememberErr(),
MethodsCalled: []string{"GetEmailSession", "UpdateUser", "DeleteEmailSession", "CreateLogin", "CreateSession"},
MethodsCalled: []string{"GetEmailSession", "UpdateUser", "DeleteEmailSession", "CreateSubscriber", "CreateSession"},
ExpectedErr: "Unable to create new session",
},
{
Expand All @@ -465,7 +465,7 @@ var createProfileTests = []struct {
getEmailSessionReturn: getEmailSessionSuccess(),
LoginReturn: loginSuccess(),
CreateSessionReturn: sessionRemember(futureTime, futureTime),
MethodsCalled: []string{"GetEmailSession", "UpdateUser", "DeleteEmailSession", "CreateLogin", "CreateSession", "InvalidateSession", "InvalidateRememberMe"},
MethodsCalled: []string{"GetEmailSession", "UpdateUser", "DeleteEmailSession", "CreateSubscriber", "CreateSession", "InvalidateSession", "InvalidateRememberMe"},
},
}

Expand Down
14 changes: 10 additions & 4 deletions backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ type backender interface {
UpdateUser(userID int, fullname string, company string, pictureURL string) error

// LoginBackender. Write out since it contains duplicate BackendCloser
CreateLogin(userID, dbUserID int, email, passwordHash, fullName, homeDirectory string, uidNumber, gidNumber int, mailQuota, fileQuota string) (*userLogin, error)
CreateAccount(userID, dbUserID int, email, passwordHash, fullName string) (*userLogin, error)
CreateSubscriber(userID, dbUserID int, email, passwordHash, fullName, homeDirectory string, uidNumber, gidNumber int, mailQuota, fileQuota string) (*userLogin, error)
Login(email, password string) (*userLogin, error)
UpdateEmail(email string, password string, newEmail string) (*loginSession, error)
UpdatePassword(email string, oldPassword string, newPassword string) (*loginSession, error)
Expand All @@ -47,7 +48,8 @@ type userBackender interface {
}

type loginBackender interface {
CreateLogin(userID, dbUserID int, email, passwordHash, fullName, homeDirectory string, uidNumber, gidNumber int, mailQuota, fileQuota string) (*userLogin, error)
CreateAccount(userID, dbUserID int, email, passwordHash, fullName string) (*userLogin, error)
CreateSubscriber(userID, dbUserID int, email, passwordHash, fullName, homeDirectory string, uidNumber, gidNumber int, mailQuota, fileQuota string) (*userLogin, error)
Login(email, password string) (*userLogin, error)
UpdateEmail(email string, password string, newEmail string) (*loginSession, error)
UpdatePassword(email string, oldPassword string, newPassword string) (*loginSession, error)
Expand Down Expand Up @@ -213,8 +215,12 @@ func (b *backend) UpdateUser(userID int, fullname string, company string, pictur
return b.u.UpdateUser(userID, fullname, company, pictureURL)
}

func (b *backend) CreateLogin(userID, dbUserID int, email, passwordHash, fullName, homeDirectory string, uidNumber, gidNumber int, mailQuota, fileQuota string) (*userLogin, error) {
return b.l.CreateLogin(userID, dbUserID, email, passwordHash, fullName, homeDirectory, uidNumber, gidNumber, mailQuota, fileQuota)
func (b *backend) CreateAccount(userID, dbUserID int, email, passwordHash, fullName string) (*userLogin, error) {
return b.l.CreateAccount(userID, dbUserID, email, passwordHash, fullName)
}

func (b *backend) CreateSubscriber(userID, dbUserID int, email, passwordHash, fullName, homeDirectory string, uidNumber, gidNumber int, mailQuota, fileQuota string) (*userLogin, error) {
return b.l.CreateSubscriber(userID, dbUserID, email, passwordHash, fullName, homeDirectory, uidNumber, gidNumber, mailQuota, fileQuota)
}

func (b *backend) UpdateEmail(email string, password string, newEmail string) (*loginSession, error) {
Expand Down
15 changes: 13 additions & 2 deletions backendLDAPLogin.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,19 @@ func (l *backendLDAPLogin) Login(email, password string) (*userLogin, error) {
}

/**************** TODO: create different type of user if not using file and mail quotas **********************/
func (l *backendLDAPLogin) CreateLogin(userID, dbUserID int, email, passwordHash, fullName, homeDirectory string, uidNumber, gidNumber int, mailQuota, fileQuota string) (*userLogin, error) {
req := ldap.NewAddRequest("uid=" + email + ",ou=Users,dc=endfirst,dc=com")
func (l *backendLDAPLogin) CreateAccount(userID, dbUserID int, email, passwordHash, fullName string) (*userLogin, error) {
req := ldap.NewAddRequest("uid=" + email + "," + l.baseDn)
req.Attribute("objectClass", []string{"endfirstAccount"})
req.Attribute("uid", []string{email})
req.Attribute("dbUserId", []string{strconv.Itoa(dbUserID)})
req.Attribute("cn", []string{fullName})
req.Attribute("userPassword", []string{passwordHash})
err := l.db.Execute(req)
return &userLogin{}, err
}

func (l *backendLDAPLogin) CreateSubscriber(userID, dbUserID int, email, passwordHash, fullName, homeDirectory string, uidNumber, gidNumber int, mailQuota, fileQuota string) (*userLogin, error) {
req := ldap.NewAddRequest("uid=" + email + "," + l.baseDn)
req.Attribute("objectClass", []string{"endfirstAccount", "endfirstSubscriber"})
req.Attribute("uid", []string{email})
req.Attribute("dbUserId", []string{strconv.Itoa(dbUserID)})
Expand Down
4 changes: 2 additions & 2 deletions backendLDAPLogin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ func TestLdapLogin(t *testing.T) {
}
}

func TestLdapCreateLogin(t *testing.T) {
func TestLdapCreateSubscriber(t *testing.T) {
m := onedb.NewMock(nil, nil, nil)
l := backendLDAPLogin{db: m}
_, err := l.CreateLogin(1, 1, "email", "hash", "name", "homeDir", 1, 1, "mailQuota", "fileQuota")
_, err := l.CreateSubscriber(1, 1, "email", "hash", "name", "homeDir", 1, 1, "mailQuota", "fileQuota")
if err != nil {
t.Error("expected success")
}
Expand Down
9 changes: 8 additions & 1 deletion backendMemory.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,14 @@ func (m *backendMemory) UpdateUser(userID int, fullname string, company string,
return nil
}

func (m *backendMemory) CreateLogin(userID, dbUserID int, email, passwordHash, fullName, homeDirectory string, uidNumber, gidNumber int, mailQuota, fileQuota string) (*userLogin, error) {
func (m *backendMemory) CreateAccount(userID, dbUserID int, email, passwordHash, fullName string) (*userLogin, error) {
login := userLoginMemory{userID, email, fullName, passwordHash}
m.Logins = append(m.Logins, &login)

return &userLogin{userID, email, fullName}, nil
}

func (m *backendMemory) CreateSubscriber(userID, dbUserID int, email, passwordHash, fullName, homeDirectory string, uidNumber, gidNumber int, mailQuota, fileQuota string) (*userLogin, error) {
login := userLoginMemory{userID, email, fullName, passwordHash}
m.Logins = append(m.Logins, &login)

Expand Down
4 changes: 2 additions & 2 deletions backendMemory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ func TestMemoryUpdateUser(t *testing.T) {
}
}

func TestMemoryCreateLogin(t *testing.T) {
func TestMemoryCreateSubscriber(t *testing.T) {
backend := newBackendMemory(&hashStore{}).(*backendMemory)
if login, err := backend.CreateLogin(1, 1, "email", "passwordHash", "fullName", "homeDirectory", 1, 1, "mailQuota", "fileQuota"); err != nil || login.Email != "email" {
if login, err := backend.CreateSubscriber(1, 1, "email", "passwordHash", "fullName", "homeDirectory", 1, 1, "mailQuota", "fileQuota"); err != nil || login.Email != "email" {
t.Error("expected valid login", login)
}
}
Expand Down
18 changes: 13 additions & 5 deletions backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ func TestBackendUpdateUser(t *testing.T) {
}
}

func TestBackendCreateLogin(t *testing.T) {
func TestBackendCreateSubscriber(t *testing.T) {
m := &mockBackend{CreateLoginReturn: loginErr()}
b := backend{u: m, l: m, s: m}
b.CreateLogin(1, 1, "email", "hash", "name", "homeDir", 1, 1, "quota", "fileQuota")
if len(m.MethodsCalled) != 1 || m.MethodsCalled[0] != "CreateLogin" {
b.CreateSubscriber(1, 1, "email", "hash", "name", "homeDir", 1, 1, "quota", "fileQuota")
if len(m.MethodsCalled) != 1 || m.MethodsCalled[0] != "CreateSubscriber" {
t.Error("Expected it would call backend", m.MethodsCalled)
}
}
Expand Down Expand Up @@ -331,8 +331,16 @@ func (b *mockBackend) UpdateUser(userID int, fullname, company, pictureURL strin
return b.ErrReturn
}

func (b *mockBackend) CreateLogin(userID, dbUserID int, email, passwordHash, fullName, homeDirectory string, uidNumber, gidNumber int, mailQuota, fileQuota string) (*userLogin, error) {
b.MethodsCalled = append(b.MethodsCalled, "CreateLogin")
func (b *mockBackend) CreateAccount(userID, dbUserID int, email, passwordHash, fullName string) (*userLogin, error) {
b.MethodsCalled = append(b.MethodsCalled, "CreateAccount")
if b.CreateLoginReturn == nil {
return nil, errors.New("CreateLoginReturn not initialized")
}
return b.CreateLoginReturn.Login, b.CreateLoginReturn.Err
}

func (b *mockBackend) CreateSubscriber(userID, dbUserID int, email, passwordHash, fullName, homeDirectory string, uidNumber, gidNumber int, mailQuota, fileQuota string) (*userLogin, error) {
b.MethodsCalled = append(b.MethodsCalled, "CreateSubscriber")
if b.CreateLoginReturn == nil {
return nil, errors.New("CreateLoginReturn not initialized")
}
Expand Down

0 comments on commit 3ca9112

Please sign in to comment.