func TestGet(t *testing.T) { render := new(tests.MockRender) repo := new(tests.MockDoorRepository) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("DoorRepository").Return(repo) repositories.On("DB").Return(db) params := martini.Params{ "id": "33", } door := &doorbot.Door{ ID: 33, Name: "ACME", } render.On("JSON", http.StatusOK, DoorViewModel{Door: door}).Return(nil) repo.On("Find", db, uint(33)).Return(door, nil) Get(render, repositories, params) 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 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 TestPut(t *testing.T) { render := new(tests.MockRender) repo := new(tests.MockAccountRepository) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("AccountRepository").Return(repo) repositories.On("DB").Return(db) postAccount := &doorbot.Account{ Name: "Romanian Landlords", } repoAccount := &doorbot.Account{ ID: 5555, Name: "ACME", IsEnabled: true, } session := &auth.Authorization{ Type: auth.AuthorizationAdministrator, } repo.On("Update", db, repoAccount).Return(true, nil) render.On("JSON", http.StatusOK, AccountViewModel{Account: repoAccount}).Return() Put(render, repoAccount, repositories, AccountViewModel{Account: postAccount}, session) assert.Equal(t, "Romanian Landlords", repoAccount.Name) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) }
func TestPost(t *testing.T) { render := new(tests.MockRender) repo := new(tests.MockAccountRepository) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("AccountRepository").Return(repo) repositories.On("DB").Return(db) account := &doorbot.Account{ Name: "ACME", Host: "derp", } admin := &doorbot.Administrator{} // nil var findByHostReponse *doorbot.Account repo.On("Create", db, account).Return(nil) repo.On("FindByHost", db, "derp").Return(findByHostReponse, nil) render.On("JSON", http.StatusCreated, AccountViewModel{Account: account}).Return() Post(render, repositories, AccountViewModel{Account: account}, admin) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) }
func TestIndex(t *testing.T) { accounts := []*doorbot.Account{ &doorbot.Account{ ID: 1, Name: "ACME", IsEnabled: true, }, } admin := &doorbot.Administrator{} render := new(tests.MockRender) repo := new(tests.MockAccountRepository) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("AccountRepository").Return(repo) repositories.On("DB").Return(db) repo.On("All", db).Return(accounts, nil) render.On("JSON", http.StatusOK, AccountsViewModel{Accounts: accounts}).Return(nil) Index(render, repositories, admin) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) }
func TestDelete(t *testing.T) { render := new(tests.MockRender) repo := new(tests.MockAccountRepository) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("AccountRepository").Return(repo) repositories.On("DB").Return(db) params := martini.Params{ "id": "33", } account := &doorbot.Account{ ID: 33, Name: "ACME", } admin := &doorbot.Administrator{} repo.On("Find", db, uint(33)).Return(account, nil) repo.On("Delete", db, account).Return(true, nil) render.On("Status", http.StatusNoContent).Return() Delete(render, repositories, params, admin) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) }
func TestGetNotFound(t *testing.T) { session := &auth.Authorization{ Type: auth.AuthorizationPerson, Person: &doorbot.Person{ AccountType: doorbot.AccountOwner, }, } 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) params := martini.Params{ "id": "33", } repo.On("Find", db, uint64(33)).Return(person, nil) render.On("JSON", http.StatusNotFound, doorbot.NewEntityNotFoundResponse([]string{"The specified person does not exists"})).Return() Get(render, repositories, params, session) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) repositories.Mock.AssertExpectations(t) }
func TestDeleteNotFound(t *testing.T) { var door *doorbot.Door repo := new(tests.MockDoorRepository) render := new(tests.MockRender) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("DoorRepository").Return(repo) repositories.On("DB").Return(db) params := martini.Params{ "id": "44", } repo.On("Find", db, uint(44)).Return(door, nil) render.On("JSON", http.StatusNotFound, doorbot.NewEntityNotFoundResponse([]string{"The specified door does not exists"})).Return() Delete(render, repositories, params) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) repositories.Mock.AssertExpectations(t) }
func TestIndex(t *testing.T) { doors := []*doorbot.Door{ &doorbot.Door{ ID: 1, AccountID: 1, Name: "A", }, } render := new(tests.MockRender) repo := new(tests.MockDoorRepository) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("DoorRepository").Return(repo) repositories.On("DB").Return(db) repo.On("All", db).Return(doors, nil) render.On("JSON", http.StatusOK, DoorsViewModel{Doors: doors}).Return() Index(render, repositories) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) repositories.Mock.AssertExpectations(t) }
func TestPutNotFound(t *testing.T) { var device *doorbot.Device render := new(tests.MockRender) repo := new(tests.MockDeviceRepository) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("DeviceRepository").Return(repo) repositories.On("DB").Return(db) params := martini.Params{ "id": "44", } postDevice := &doorbot.Device{ Name: "Chicken Nick", } repo.On("Find", db, uint64(44)).Return(device, nil) render.On("JSON", http.StatusNotFound, doorbot.NewEntityNotFoundResponse([]string{"The specified device does not exists"})).Return() Put(render, repositories, params, DeviceViewModel{Device: postDevice}) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) repositories.Mock.AssertExpectations(t) }
func TestPostCreateError(t *testing.T) { render := new(tests.MockRender) repo := new(tests.MockDoorRepository) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("DoorRepository").Return(repo) repositories.On("DB").Return(db) repositories.On("AccountScope").Return(uint(1)) door := &doorbot.Door{ Name: "ACME", } repo.On("Create", db, door).Return(errors.New("errooor")) render.On("JSON", http.StatusInternalServerError, doorbot.NewInternalServerErrorResponse([]string{})).Return() Post(render, repositories, DoorViewModel{Door: door}) 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 TestDeleteFailed(t *testing.T) { repo := new(tests.MockDoorRepository) render := new(tests.MockRender) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("DoorRepository").Return(repo) repositories.On("DB").Return(db) repositories.On("AccountScope").Return(uint(1)) params := martini.Params{ "id": "55", } door := &doorbot.Door{ ID: 55, Name: "ACME", } repo.On("Find", db, uint(55)).Return(door, nil) repo.On("Delete", db, door).Return(false, errors.New("error")) render.On("Status", http.StatusInternalServerError).Return() Delete(render, repositories, params) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) repositories.Mock.AssertExpectations(t) }
func TestDelete(t *testing.T) { render := new(tests.MockRender) repo := new(tests.MockDoorRepository) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("DoorRepository").Return(repo) repositories.On("DB").Return(db) repositories.On("AccountScope").Return(uint(1)) params := martini.Params{ "id": "33", } door := &doorbot.Door{ ID: 33, Name: "ACME", } repo.On("Find", db, uint(33)).Return(door, nil) repo.On("Delete", db, door).Return(true, nil) render.On("Status", http.StatusNoContent).Return() Delete(render, repositories, params) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) repositories.Mock.AssertExpectations(t) }
func TestGetNotFound(t *testing.T) { var account *doorbot.Account session := &auth.Authorization{} render := new(tests.MockRender) repo := new(tests.MockAccountRepository) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("DB").Return(db) repositories.On("AccountRepository").Return(repo) repositories.On("AccountScope").Return(1) params := martini.Params{ "id": "33", } repo.On("Find", db, uint(33)).Return(account, nil) render.On("JSON", http.StatusNotFound, doorbot.NewEntityNotFoundResponse([]string{})).Return() Get(render, repositories, params, session) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) }
func TestPost(t *testing.T) { render := new(tests.MockRender) repo := new(tests.MockDoorRepository) repositories := new(tests.MockRepositories) repositories.On("DoorRepository").Return(repo) repositories.On("AccountScope").Return(uint(1)) db := new(tests.MockExecutor) repositories.On("DB").Return(db) door := &doorbot.Door{ Name: "ACME", } repo.On("Create", db, door).Return(nil) render.On("JSON", http.StatusCreated, DoorViewModel{Door: door}).Return() Post(render, repositories, DoorViewModel{Door: door}) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) repositories.Mock.AssertExpectations(t) }
func TestDisable(t *testing.T) { render := new(tests.MockRender) repo := new(tests.MockDeviceRepository) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("DeviceRepository").Return(repo) repositories.On("DB").Return(db) repositories.On("AccountScope").Return(uint(1)) params := martini.Params{ "id": "4443", } device := &doorbot.Device{ ID: 4443, IsEnabled: false, } repo.On("Find", db, uint64(4443)).Return(device, nil) repo.On("Enable", db, device, false).Return(true, nil) render.On("Status", http.StatusNoContent).Return() Disable(render, repositories, params) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) repositories.Mock.AssertExpectations(t) }
func TestGetNotFound(t *testing.T) { var device *doorbot.Device render := new(tests.MockRender) repo := new(tests.MockDeviceRepository) params := martini.Params{ "id": "33", } db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("DeviceRepository").Return(repo) repositories.On("DB").Return(db) repositories.On("AccountScope").Return(uint(1)) repo.On("Find", db, uint64(33)).Return(device, nil) render.On("JSON", http.StatusNotFound, doorbot.NewEntityNotFoundResponse([]string{"The specified device does not exists"})).Return() Get(render, repositories, params) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) repositories.Mock.AssertExpectations(t) }
func TestPutFailed(t *testing.T) { render := new(tests.MockRender) repo := new(tests.MockDoorRepository) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("DoorRepository").Return(repo) repositories.On("DB").Return(db) repositories.On("AccountScope").Return(uint(1)) params := martini.Params{ "id": "5555", } postDoor := &doorbot.Door{ Name: "Romanian Landlords", } repoDoor := &doorbot.Door{ ID: 5555, Name: "ACME", } repo.On("Find", db, uint(5555)).Return(repoDoor, nil) repo.On("Update", db, repoDoor).Return(false, errors.New("failed")) render.On("JSON", http.StatusInternalServerError, doorbot.NewInternalServerErrorResponse([]string{})).Return() Put(render, repositories, params, DoorViewModel{Door: postDoor}) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) repositories.Mock.AssertExpectations(t) }
func TestDeleteFailed(t *testing.T) { repo := new(tests.MockAccountRepository) render := new(tests.MockRender) admin := &doorbot.Administrator{} db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("AccountRepository").Return(repo) repositories.On("DB").Return(db) repositories.On("AccountScope").Return(1) params := martini.Params{ "id": "55", } account := &doorbot.Account{ ID: 55, Name: "ACME", } repo.On("Find", db, uint(55)).Return(account, nil) repo.On("Delete", db, account).Return(false, errors.New("error")) render.On("JSON", http.StatusInternalServerError, doorbot.NewInternalServerErrorResponse([]string{})).Return() Delete(render, repositories, params, admin) render.Mock.AssertExpectations(t) repo.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 TestPutFailed(t *testing.T) { render := new(tests.MockRender) repo := new(tests.MockAccountRepository) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("AccountRepository").Return(repo) repositories.On("DB").Return(db) repositories.On("AccountScope").Return(1) postAccount := &doorbot.Account{ Name: "Romanian Landlords", } repoAccount := &doorbot.Account{ ID: 5555, Name: "ACME", IsEnabled: true, } session := &auth.Authorization{ Type: auth.AuthorizationAdministrator, } repo.On("Update", db, repoAccount).Return(false, errors.New("failed")) render.On("JSON", http.StatusInternalServerError, doorbot.NewInternalServerErrorResponse([]string{})).Return() Put(render, repoAccount, repositories, AccountViewModel{Account: postAccount}, session) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) }
func TestDeleteNotFound(t *testing.T) { var account *doorbot.Account admin := &doorbot.Administrator{} repo := new(tests.MockAccountRepository) render := new(tests.MockRender) repositories := new(tests.MockRepositories) repositories.On("AccountRepository").Return(repo) repositories.On("AccountScope").Return(1) db := new(tests.MockExecutor) repositories.On("DB").Return(db) params := martini.Params{ "id": "44", } repo.On("Find", db, uint(44)).Return(account, nil) render.On("JSON", http.StatusNotFound, doorbot.NewEntityNotFoundResponse([]string{"The specified account does not exists."})).Return() Delete(render, repositories, params, admin) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) }
func TestPostCreateError(t *testing.T) { render := new(tests.MockRender) repo := new(tests.MockAccountRepository) db := new(tests.MockExecutor) admin := &doorbot.Administrator{} repositories := new(tests.MockRepositories) repositories.On("AccountRepository").Return(repo) repositories.On("DB").Return(db) repositories.On("AccountScope").Return(1) account := &doorbot.Account{ Name: "ACME", Host: "derp", } // nil var findByHostReponse *doorbot.Account repo.On("Create", db, account).Return(errors.New("errooor")) repo.On("FindByHost", db, "derp").Return(findByHostReponse, nil) render.On("JSON", http.StatusInternalServerError, doorbot.NewInternalServerErrorResponse([]string{})).Return() Post(render, repositories, AccountViewModel{Account: account}, admin) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) }
func TestDeleteInvalidID(t *testing.T) { repo := new(tests.MockAccountRepository) render := new(tests.MockRender) admin := &doorbot.Administrator{} repositories := new(tests.MockRepositories) repositories.On("AccountRepository").Return(repo) params := martini.Params{ "id": "help", } render.On("JSON", http.StatusBadRequest, doorbot.NewBadRequestErrorResponse([]string{"The id must be an unsigned integer"})).Return() Delete(render, repositories, params, admin) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) }
func TestGetParseIntError(t *testing.T) { render := new(tests.MockRender) repo := new(tests.MockAccountRepository) repositories := new(tests.MockRepositories) repositories.On("AccountRepository").Return(repo) session := &auth.Authorization{} params := martini.Params{ "id": "help", } render.On("JSON", http.StatusBadRequest, doorbot.NewBadRequestErrorResponse([]string{"The id must be an unsigned integer"})).Return() Get(render, repositories, params, session) render.Mock.AssertExpectations(t) repo.Mock.AssertExpectations(t) }
func TestGetNotOwner(t *testing.T) { render := new(tests.MockRender) repo := new(tests.MockAccountRepository) db := new(tests.MockExecutor) repositories := new(tests.MockRepositories) repositories.On("AccountRepository").Return(repo) repositories.On("DB").Return(db) params := martini.Params{ "id": "33", } account := &doorbot.Account{ ID: 33, Name: "ACME", } resp := PublicAccount{ ID: 33, Name: "ACME", } session := &auth.Authorization{ Type: auth.AuthorizationPerson, Person: &doorbot.Person{ ID: 3456, }, } render.On("JSON", http.StatusOK, PublicAccountViewModel{Account: resp}).Return(nil) repo.On("Find", db, uint(33)).Return(account, nil) Get(render, repositories, params, session) render.Mock.AssertExpectations(t) repo.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) }