From 3ca91128368dac3aaae5d2bd7030692a7d3ecef3 Mon Sep 17 00:00:00 2001 From: Rob Archibald Date: Tue, 31 Jan 2017 01:00:31 -0800 Subject: [PATCH] Split createlogin into createAccount and createSubscriber --- authStore.go | 2 +- authStore_test.go | 6 +++--- backend.go | 14 ++++++++++---- backendLDAPLogin.go | 15 +++++++++++++-- backendLDAPLogin_test.go | 4 ++-- backendMemory.go | 9 ++++++++- backendMemory_test.go | 4 ++-- backend_test.go | 18 +++++++++++++----- 8 files changed, 52 insertions(+), 20 deletions(-) diff --git a/authStore.go b/authStore.go index ea9e678..89869ce 100644 --- a/authStore.go +++ b/authStore.go @@ -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) } diff --git a/authStore_test.go b/authStore_test.go index 5661b0e..945c0c0 100644 --- a/authStore_test.go +++ b/authStore_test.go @@ -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", }, { @@ -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", }, { @@ -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"}, }, } diff --git a/backend.go b/backend.go index 4df78b5..57414e6 100644 --- a/backend.go +++ b/backend.go @@ -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) @@ -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) @@ -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) { diff --git a/backendLDAPLogin.go b/backendLDAPLogin.go index 3be66ad..75926f7 100644 --- a/backendLDAPLogin.go +++ b/backendLDAPLogin.go @@ -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)}) diff --git a/backendLDAPLogin_test.go b/backendLDAPLogin_test.go index 8c3727f..4bc6e66 100644 --- a/backendLDAPLogin_test.go +++ b/backendLDAPLogin_test.go @@ -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") } diff --git a/backendMemory.go b/backendMemory.go index 59f5f0f..b42d8e9 100644 --- a/backendMemory.go +++ b/backendMemory.go @@ -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) diff --git a/backendMemory_test.go b/backendMemory_test.go index 5526592..2afcffc 100644 --- a/backendMemory_test.go +++ b/backendMemory_test.go @@ -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) } } diff --git a/backend_test.go b/backend_test.go index 5f19c1a..b0ed05b 100644 --- a/backend_test.go +++ b/backend_test.go @@ -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) } } @@ -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") }