Exemplo n.º 1
0
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)
}
Exemplo n.º 2
0
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)
}
Exemplo n.º 3
0
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)
}
Exemplo n.º 4
0
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)
}