def test_submit_time_dialog_for_new_user_should_call_slack_api_and_return( self): # arrange def slack_client_api_call_side_effect(method, **kwargs): if method == "users.info": return { "ok": True, "user": { "profile": { "email": "*****@*****.**", "real_name_normalized": "User Name" } } } if method == "dialog.open": return {"ok": True} self.mock_slack_client.api_call.side_effect = slack_client_api_call_side_effect command_body = {'trigger_id': '123abc', 'user_id': 'user123'} action = mock.MagicMock() action.value.return_value = '2019-01-16' self.mock_project_service.get_projects.return_value = [ Project(), Project() ] self.mock_project_service.get_projects_by_user.return_value = [ Project(), Project() ] # act self.handler.show_dialog(command_body, None, action) # assert self.assertEqual(self.mock_slack_client.api_call.call_count, 1)
def test_update_project_should_change_name(self): # Arrange project = Project() project.name = 'old-name' self.mock_project_service.get_project_by_id.return_value = project # Act updated_project = self.api_service.update_project(1, 'new-name') # Assert self.assertEqual(updated_project['project_name'], 'new-name')
def test_create_project_should_create(self): # Arrange project = Project() project.name = 'project' self.mock_project_service.create_project.return_value = project # Act result = self.api_service.create_project('project') # Assert self.mock_project_service.create_project.assert_called_once_with( 'project') self.assertEqual(result['project_name'], 'project')
def get_mocked_time_entry(): mock_time_entry = mock.create_autospec(TimeEntry) mock_time_entry.report_date = datetime(2018, 5, 1) mock_time_entry.comment = "comment" mock_time_entry.duration = Decimal('8.0') mock_time_entry.project = Project(name='TestPr', project_id=1) return mock_time_entry
def get_mocked_user(): mock_user = mock.create_autospec(User) mock_user.user_id = 1 mock_user.username = "******" mock_user.first_name = "User Name" mock_user.user_projects = [Project(name='TestPr', project_id=1)] return mock_user
def setUp(self, mock_project_service, mock_user_service, mock_slack_client, config_mock): self.mock_user_service = mock_user_service self.mock_project_service = mock_project_service self.mock_slack_client = mock_slack_client self.config_mock = config_mock self.mock_project_service.get_projects.return_value = [ Project(name='TestPr', project_id=1), Project(name='TestPr2', project_id=2) ] self.mock_project_service.get_project_by_id.return_value = None self.mock_user_service.get_user_by_id.return_value = None self.handler = SubmitTimeCommandHandler( mock.create_autospec(Config), mock.create_autospec(logging.Logger), mock_user_service, mock_slack_client, mock_project_service, mock.create_autospec(ReminderService))
def test_submit_time_dialog_for_existed_user_should_set_default_project( self): # arrange def slack_client_api_call_side_effect(method, **kwargs): if method == "users.info": return { "ok": True, "user": { "profile": { "email": "*****@*****.**", "real_name_normalized": "User Name" } } } if method == "dialog.open": self.dialog = kwargs["dialog"] return {"ok": True} self.mock_slack_client.api_call.side_effect = slack_client_api_call_side_effect self.mock_user_service.add_user.return_value = get_mocked_user() self.mock_user_service.get_user_by_email.return_value = get_mocked_user( ) users_last_time_entry = get_mocked_time_entry() self.mock_user_service.get_user_last_time_entry.return_value = users_last_time_entry command_body = {"trigger_id": "123abc", "user_id": "user123"} action = mock.MagicMock() action.value.return_value = '2019-01-16' self.mock_project_service.get_projects.return_value = [ Project(), Project() ] self.mock_project_service.get_projects_by_user.return_value = [ Project(), Project() ] # act self.handler.show_dialog(command_body, None, action) # assert self.assertEqual(self.mock_slack_client.api_call.call_count, 1) # check selected project for dialog self.assertEqual(int(self.dialog["elements"][0]["value"]), users_last_time_entry.project.project_id)
def create_project(self, project_name): new_project = Project(name=project_name) self.db.session.add(new_project) self.db.session.commit() return new_project