Example #1
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)
}
Example #2
0
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)
}