func TestPostCreateError(t *testing.T) { render := new(tests.MockRender) repo := new(tests.MockPersonRepository) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("PersonRepository").Return(repo) repositories.On("DB").Return(db) repositories.On("AccountScope").Return(uint(1)) person := &doorbot.Person{ Name: "ACME", } repo.On("Create", db, person).Return(errors.New("errooor")) render.On("JSON", http.StatusInternalServerError, doorbot.NewInternalServerErrorResponse([]string{})).Return() Post(render, repositories, PersonViewModel{Person: person}) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) repositories.Mock.AssertExpectations(t) }
func TestIndexError(t *testing.T) { session := &auth.Authorization{ Type: auth.AuthorizationPerson, Person: &doorbot.Person{ AccountType: doorbot.AccountOwner, }, } people := []*doorbot.Person{} err := errors.New("i like pasta") render := new(tests.MockRender) repo := new(tests.MockPersonRepository) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("PersonRepository").Return(repo) repositories.On("AccountScope").Return(uint(0)) repositories.On("DB").Return(db) repo.On("All", db).Return(people, err) render.On("JSON", http.StatusInternalServerError, doorbot.NewInternalServerErrorResponse([]string{})).Return() Index(render, repositories, session) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) repositories.Mock.AssertExpectations(t) }
func TestIndex(t *testing.T) { people := []*doorbot.Person{ &doorbot.Person{ ID: 1, AccountID: 1, Name: "A", }, } session := &auth.Authorization{ Type: auth.AuthorizationPerson, Person: &doorbot.Person{ AccountType: doorbot.AccountOwner, }, } render := new(tests.MockRender) repo := new(tests.MockPersonRepository) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("PersonRepository").Return(repo) repositories.On("DB").Return(db) repo.On("All", db).Return(people, nil) render.On("JSON", http.StatusOK, PeopleViewModel{People: people}).Return() Index(render, repositories, session) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) repositories.Mock.AssertExpectations(t) }
func TestDeleteNotFound(t *testing.T) { var person *doorbot.Person repo := new(tests.MockPersonRepository) render := new(tests.MockRender) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("PersonRepository").Return(repo) repositories.On("DB").Return(db) repositories.On("AccountScope").Return(uint(1)) params := martini.Params{ "id": "44", } account := &doorbot.Account{} session := &auth.Authorization{ Type: auth.AuthorizationPerson, Person: &doorbot.Person{ AccountType: doorbot.AccountOwner, }, } repo.On("Find", db, uint(44)).Return(person, nil) render.On("JSON", http.StatusNotFound, doorbot.NewEntityNotFoundResponse([]string{"The specified person does not exists"})).Return() Delete(render, repositories, params, account, session) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) repositories.Mock.AssertExpectations(t) }
func TestGet(t *testing.T) { session := &auth.Authorization{ Type: auth.AuthorizationPerson, Person: &doorbot.Person{ AccountType: doorbot.AccountOwner, }, } render := new(tests.MockRender) repo := new(tests.MockPersonRepository) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("PersonRepository").Return(repo) repositories.On("DB").Return(db) params := martini.Params{ "id": "33", } person := &doorbot.Person{ ID: 33, Name: "ACME", } render.On("JSON", http.StatusOK, PersonViewModel{Person: person}).Return(nil) repo.On("Find", db, uint(33)).Return(person, nil) Get(render, repositories, params, session) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) repositories.Mock.AssertExpectations(t) }
func TestPost(t *testing.T) { render := new(tests.MockRender) repo := new(tests.MockPersonRepository) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("PersonRepository").Return(repo) repositories.On("DB").Return(db) repositories.On("AccountScope").Return(uint(0)) person := &doorbot.Person{ Name: "ACME", } repo.On("Create", db, person).Return(nil) render.On("JSON", http.StatusCreated, PersonViewModel{Person: person}).Return() Post(render, repositories, PersonViewModel{Person: person}) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) repositories.Mock.AssertExpectations(t) }
func TestPassword(t *testing.T) { account := &doorbot.Account{ ID: 444, Name: "ACME", IsEnabled: true, } person := &doorbot.Person{ ID: 1, Name: "Cookie Monster", Email: "*****@*****.**", } passwordAuthentication := &doorbot.Authentication{ AccountID: 444, PersonID: 1, Token: "$2a$10$8XdprxFRIXCv1TC2cDjMNuQRiYkOX9PIivVpnSMM9b.1UjulLlrVm", // test } passwordRequest := PasswordRequest{ Authentication: PasswordAuthentication{ Email: "*****@*****.**", Password: "******", }, } var tokenNotFound *doorbot.Authentication render := new(tests.MockRender) personRepo := new(tests.MockPersonRepository) authRepo := new(tests.MockAuthenticationRepository) repositories := new(tests.MockRepositories) repositories.On("PersonRepository").Return(personRepo) repositories.On("AuthenticationRepository").Return(authRepo) db := new(tests.MockExecutor) repositories.On("DB").Return(db) personRepo.On("FindByEmail", db, "*****@*****.**").Return(person, nil) authRepo.On("FindByPersonIDAndProviderID", db, person.ID, auth.ProviderPassword).Return(passwordAuthentication, nil).Once() authRepo.On("FindByPersonIDAndProviderID", db, person.ID, auth.ProviderAPIToken).Return(tokenNotFound, nil).Once() authRepo.On("Create", db, mock.AnythingOfType("*doorbot.Authentication")).Return(nil).Once() render.On("JSON", http.StatusOK, mock.AnythingOfType("auth.APITokenResponse")).Return().Once() Password(render, account, repositories, passwordRequest) render.Mock.AssertExpectations(t) personRepo.Mock.AssertExpectations(t) repositories.Mock.AssertExpectations(t) authRepo.Mock.AssertExpectations(t) }
func TestNotify(t *testing.T) { render := new(tests.MockRender) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) notificator := new(tests.MockNotificator) peopleRepo := new(tests.MockPersonRepository) doorRepo := new(tests.MockDoorRepository) account := &doorbot.Account{ ID: 44, } person := &doorbot.Person{ AccountID: 44, ID: 45, Name: "John Rambo", Email: "*****@*****.**", IsVisible: true, IsAvailable: true, } door := &doorbot.Door{} notification := Notification{ DoorID: 33, PersonID: 45, } repositories.On("PersonRepository").Return(peopleRepo) repositories.On("DoorRepository").Return(doorRepo) repositories.On("DB").Return(db) peopleRepo.On("Find", db, uint(45)).Return(person, nil) doorRepo.On("Find", db, uint(33)).Return(door, nil) notificator.On("Notify", account, door, person).Return(nil) vm := ViewModel{Notification: ¬ification} render.On("JSON", http.StatusAccepted, ViewModel{Notification: ¬ification}).Return() Notify(render, account, repositories, notificator, vm) render.Mock.AssertExpectations(t) peopleRepo.Mock.AssertExpectations(t) doorRepo.Mock.AssertExpectations(t) repositories.Mock.AssertExpectations(t) notificator.Mock.AssertExpectations(t) }
func TestPasswordInvalid(t *testing.T) { person := &doorbot.Person{ ID: 1, Name: "Cookie Monster", Email: "*****@*****.**", } account := &doorbot.Account{ ID: 1, Name: "ACME", IsEnabled: true, } passwordAuthentication := &doorbot.Authentication{ AccountID: 1, PersonID: 1, Token: "$2a$10$8XdprxFRIXCv1TC2cDjMNuQRiYkOX9PIivVpnSMM9b.1UjulLlrVm", // test } passwordRequest := PasswordRequest{ Authentication: PasswordAuthentication{ Email: "*****@*****.**", Password: "******", }, } render := new(tests.MockRender) personRepo := new(tests.MockPersonRepository) authRepo := new(tests.MockAuthenticationRepository) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("PersonRepository").Return(personRepo) repositories.On("AuthenticationRepository").Return(authRepo) repositories.On("DB").Return(db) personRepo.On("FindByEmail", db, "*****@*****.**").Return(person, nil) authRepo.On("FindByPersonIDAndProviderID", db, person.ID, auth.ProviderPassword).Return(passwordAuthentication, nil) render.On("JSON", http.StatusUnauthorized, doorbot.NewUnauthorizedErrorResponse([]string{"Invalid email or password"})).Return() Password(render, account, repositories, passwordRequest) render.Mock.AssertExpectations(t) personRepo.Mock.AssertExpectations(t) authRepo.Mock.AssertExpectations(t) repositories.Mock.AssertExpectations(t) }
func TestRegister(t *testing.T) { render := new(tests.MockRender) notificator := new(tests.MockNotificator) accountRepo := new(tests.MockAccountRepository) authRepo := new(tests.MockAuthenticationRepository) personRepo := new(tests.MockPersonRepository) db := new(tests.MockExecutor) tx := new(tests.MockTransaction) config := &doorbot.DoorbotConfig{} repositories := new(tests.MockRepositories) repositories.On("AccountRepository").Return(accountRepo) repositories.On("PersonRepository").Return(personRepo) repositories.On("AuthenticationRepository").Return(authRepo) repositories.On("SetAccountScope", uint(0)).Return() repositories.On("DB").Return(db) repositories.On("Transaction").Return(tx, nil) vm := RegisterViewModel{ Account: AccountRegisterRequest{ Name: "ACME", }, } var noAccount *doorbot.Account accountRepo.On("FindByHost", db, mock.AnythingOfType("string")).Return(noAccount, nil) accountRepo.On("Create", tx, mock.AnythingOfType("*doorbot.Account")).Return(nil) personRepo.On("Create", tx, mock.AnythingOfType("*doorbot.Person")).Return(nil) authRepo.On("Create", tx, mock.AnythingOfType("*doorbot.Authentication")).Return(nil) notificator.On("AccountCreated", mock.AnythingOfType("*doorbot.Account"), mock.AnythingOfType("*doorbot.Person"), mock.AnythingOfType("string")).Return() tx.On("Commit").Return(nil) render.On("JSON", http.StatusCreated, mock.AnythingOfType("AccountViewModel")).Return() Register(render, config, repositories, notificator, vm) render.Mock.AssertExpectations(t) accountRepo.Mock.AssertExpectations(t) personRepo.Mock.AssertExpectations(t) authRepo.Mock.AssertExpectations(t) }
func TestAuthPassword_UserNotFound(t *testing.T) { requestBody, _ := json.Marshal(auth.PasswordRequest{ Authentication: auth.PasswordAuthentication{ Email: "*****@*****.**", Password: "******", }, }) req, _ := http.NewRequest("POST", "/api/auth/password", bytes.NewBuffer(requestBody)) req.Header.Add("Content-Type", "application/json") req.Header.Add("Host", "account.example.com") req.Header.Add("Authorization", "dashboard gatekeeper") rec := httptest.NewRecorder() server := newServer() account := &doorbot.Account{ ID: 45, } var person *doorbot.Person accountRepo := new(tests.MockAccountRepository) authRepo := new(tests.MockAuthenticationRepository) personRepo := new(tests.MockPersonRepository) db := new(tests.MockExecutor) repos := getDependency(server, (*doorbot.Repositories)(nil)).(*tests.MockRepositories) repos.On("SetAccountScope", uint(45)).Return() repos.On("AccountRepository").Return(accountRepo) repos.On("AuthenticationRepository").Return(authRepo) repos.On("PersonRepository").Return(personRepo) repos.On("DB").Return(db) accountRepo.On("FindByHost", db, "account").Return(account, nil) personRepo.On("FindByEmail", db, "*****@*****.**").Return(person, nil) server.ServeHTTP(rec, req) assert.Equal(t, http.StatusUnauthorized, rec.Code) repos.AssertExpectations(t) personRepo.AssertExpectations(t) accountRepo.AssertExpectations(t) }
func TestPut(t *testing.T) { render := new(tests.MockRender) repo := new(tests.MockPersonRepository) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("PersonRepository").Return(repo) repositories.On("DB").Return(db) repositories.On("AccountScope").Return(uint(1)) params := martini.Params{ "id": "5555", } postPerson := &doorbot.Person{ Name: "Romanian Landlords", } repoPerson := &doorbot.Person{ ID: 5555, Name: "ACME", AccountType: doorbot.AccountOwner, } session := &auth.Authorization{ Type: auth.AuthorizationPerson, Person: repoPerson, } repo.On("Find", db, uint(5555)).Return(repoPerson, nil) repo.On("Update", db, repoPerson).Return(true, nil) render.On("JSON", http.StatusOK, PersonViewModel{Person: repoPerson}).Return() Put(render, repositories, params, PersonViewModel{Person: postPerson}, session) assert.Equal(t, "Romanian Landlords", repoPerson.Name) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) repositories.Mock.AssertExpectations(t) }
func TestPutFailed(t *testing.T) { render := new(tests.MockRender) repo := new(tests.MockPersonRepository) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("PersonRepository").Return(repo) repositories.On("DB").Return(db) repositories.On("AccountScope").Return(uint(1)) params := martini.Params{ "id": "5555", } postPerson := &doorbot.Person{ Name: "Romanian Landlords", } repoPerson := &doorbot.Person{ ID: 5555, Name: "ACME", AccountType: doorbot.AccountOwner, } session := &auth.Authorization{ Type: auth.AuthorizationPerson, Person: repoPerson, } repo.On("Find", db, uint(5555)).Return(repoPerson, nil) repo.On("Update", db, repoPerson).Return(false, errors.New("failed")) render.On("JSON", http.StatusInternalServerError, doorbot.NewInternalServerErrorResponse([]string{})).Return() Put(render, repositories, params, PersonViewModel{Person: postPerson}, session) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) repositories.Mock.AssertExpectations(t) }
func TestDeleteFailed(t *testing.T) { repo := new(tests.MockPersonRepository) render := new(tests.MockRender) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("PersonRepository").Return(repo) repositories.On("DB").Return(db) repositories.On("AccountScope").Return(uint(1)) params := martini.Params{ "id": "55", } person := &doorbot.Person{ ID: 55, Name: "ACME", } account := &doorbot.Account{} session := &auth.Authorization{ Type: auth.AuthorizationPerson, Person: &doorbot.Person{ AccountType: doorbot.AccountOwner, }, } repo.On("Find", db, uint(55)).Return(person, nil) repo.On("Delete", db, person).Return(false, errors.New("error")) render.On("Status", http.StatusInternalServerError).Return() Delete(render, repositories, params, account, session) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) repositories.Mock.AssertExpectations(t) }
func TestDelete(t *testing.T) { render := new(tests.MockRender) repo := new(tests.MockPersonRepository) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("PersonRepository").Return(repo) repositories.On("DB").Return(db) repositories.On("AccountScope").Return(uint(1)) params := martini.Params{ "id": "33", } person := &doorbot.Person{ ID: 33, Name: "ACME", } account := &doorbot.Account{} session := &auth.Authorization{ Type: auth.AuthorizationPerson, Person: &doorbot.Person{ AccountType: doorbot.AccountOwner, }, } repo.On("Find", db, uint(33)).Return(person, nil) repo.On("Delete", db, person).Return(true, nil) render.On("Status", http.StatusNoContent).Return() Delete(render, repositories, params, account, session) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) repositories.Mock.AssertExpectations(t) }
func TestPutNotFound(t *testing.T) { var person *doorbot.Person render := new(tests.MockRender) repo := new(tests.MockPersonRepository) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("PersonRepository").Return(repo) repositories.On("DB").Return(db) repositories.On("AccountScope").Return(uint(1)) params := martini.Params{ "id": "44", } postPerson := &doorbot.Person{ Name: "Chicken Nick", } session := &auth.Authorization{ Type: auth.AuthorizationPerson, Person: &doorbot.Person{ ID: 3, AccountType: doorbot.AccountManager, }, } repo.On("Find", db, uint(44)).Return(person, nil) render.On("JSON", http.StatusNotFound, doorbot.NewEntityNotFoundResponse([]string{"The specified person does not exists"})).Return() Put(render, repositories, params, PersonViewModel{postPerson}, session) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) repositories.Mock.AssertExpectations(t) }
func TestPasswordPersonNotFound(t *testing.T) { var person *doorbot.Person account := &doorbot.Account{ ID: 1, Name: "ACME", IsEnabled: true, } passwordRequest := PasswordRequest{ Authentication: PasswordAuthentication{ Email: "*****@*****.**", Password: "******", }, } render := new(tests.MockRender) personRepo := new(tests.MockPersonRepository) authRepo := new(tests.MockAuthenticationRepository) repositories := new(tests.MockRepositories) repositories.On("PersonRepository").Return(personRepo) repositories.On("AuthenticationRepository").Return(authRepo) db := new(tests.MockExecutor) repositories.On("DB").Return(db) personRepo.On("FindByEmail", db, "*****@*****.**").Return(person, nil) render.On("JSON", http.StatusUnauthorized, doorbot.NewUnauthorizedErrorResponse([]string{"Invalid email or password"})).Return() Password(render, account, repositories, passwordRequest) render.Mock.AssertExpectations(t) personRepo.Mock.AssertExpectations(t) authRepo.Mock.AssertExpectations(t) repositories.Mock.AssertExpectations(t) }
func TestPasswordReuseToken(t *testing.T) { account := &doorbot.Account{ ID: 1, Name: "ACME", IsEnabled: true, } person := &doorbot.Person{ ID: 1, Name: "Cookie Monster", Email: "*****@*****.**", } passwordAuthentication := &doorbot.Authentication{ AccountID: 1, PersonID: 1, Token: "$2a$10$8XdprxFRIXCv1TC2cDjMNuQRiYkOX9PIivVpnSMM9b.1UjulLlrVm", // test } apiTokenAuthentication := &doorbot.Authentication{ AccountID: 1, PersonID: 1, Token: "i-like-pasta", } passwordRequest := PasswordRequest{ Authentication: PasswordAuthentication{ Email: "*****@*****.**", Password: "******", }, } token := APITokenResponse{ Authentication: TokenAuthentication{Token: "1.i-like-pasta"}, Person: person, } render := new(tests.MockRender) personRepo := new(tests.MockPersonRepository) authRepo := new(tests.MockAuthenticationRepository) repositories := new(tests.MockRepositories) repositories.On("PersonRepository").Return(personRepo) repositories.On("AuthenticationRepository").Return(authRepo) db := new(tests.MockExecutor) repositories.On("DB").Return(db) personRepo.On("FindByEmail", db, "*****@*****.**").Return(person, nil) authRepo.On("FindByPersonIDAndProviderID", db, person.ID, auth.ProviderPassword).Return(passwordAuthentication, nil) authRepo.On("FindByPersonIDAndProviderID", db, person.ID, auth.ProviderAPIToken).Return(apiTokenAuthentication, nil) render.On("JSON", http.StatusOK, token).Return() Password(render, account, repositories, passwordRequest) render.Mock.AssertExpectations(t) personRepo.Mock.AssertExpectations(t) repositories.Mock.AssertExpectations(t) authRepo.Mock.AssertExpectations(t) }
func TestSync(t *testing.T) { personRepo := new(tests.MockPersonRepository) bridgeUserRepo := new(tests.MockBridgeUserRepository) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("PersonRepository").Return(personRepo) repositories.On("BridgeUserRepository").Return(bridgeUserRepo) repositories.On("DB").Return(db) repositories.On("AccountScope").Return(uint(1)) render := new(tests.MockRender) person := &doorbot.Person{ ID: 34, Name: "Joe", Email: "*****@*****.**", } rambo := &doorbot.Person{ Name: "Rambo", Email: "*****@*****.**", } bridgeUsers := make([]*doorbot.BridgeUser, 2) bridgeUsers[0] = &doorbot.BridgeUser{ UserID: "34", Name: "Bob", Email: "*****@*****.**", } bridgeUsers[1] = &doorbot.BridgeUser{ UserID: "35", Name: "Rambo", Email: "*****@*****.**", } registeredUsers := make([]*doorbot.BridgeUser, 1) registeredUsers[0] = &doorbot.BridgeUser{ PersonID: 34, UserID: "34", } session := &auth.Authorization{ Type: auth.AuthorizationPerson, Person: &doorbot.Person{ AccountType: doorbot.AccountOwner, }, } transaction := new(tests.MockTransaction) repositories.On("Transaction").Return(transaction, nil) transaction.On("Commit").Return(nil) bs := new(tests.MockBridges) bs.On("GetUsers", bridges.BridgeHub).Return(bridgeUsers, nil) personRepo.On("Find", transaction, uint(34)).Return(person, nil) personRepo.On("Create", transaction, rambo).Return(nil) personRepo.On("Update", transaction, person).Return(true, nil) bridgeUserRepo.On("FindByBridgeID", db, uint(1)).Return(registeredUsers, nil) bridgeUserRepo.On("Create", transaction, bridgeUsers[1]).Return(nil) render.On("Status", http.StatusNoContent).Return() Sync(render, repositories, bs, session) render.Mock.AssertExpectations(t) repositories.Mock.AssertExpectations(t) personRepo.Mock.AssertExpectations(t) bridgeUserRepo.Mock.AssertExpectations(t) bs.Mock.AssertExpectations(t) }