def test_download_incomplete_list_csv_contents(self):
        request_mock = mock.Mock()
        request_mock.GET = {Constants.ACTIVATE_BLOCK_ID_PARAMETER_NAME: 'target_stage_id'}

        target_stage = mock.Mock(spec=BaseGroupActivityStage)
        target_stage.display_name = 'Stage 1'
        target_stage.get_users_completion.return_value = ({1}, {'irrelevant'})
        all_users = [
            ReducedUserDetails(id=1, first_name="U1", last_name="U1", username='******', email="*****@*****.**"),
            ReducedUserDetails(id=2, first_name="U2", last_name="U2", username='******', email="*****@*****.**"),
            ReducedUserDetails(id=3, first_name="U3", last_name="U3", username='******', email="*****@*****.**"),
        ]

        def csv_repr(user):
            return {"Name": user.full_name, 'Email': user.email, 'Username': user.username}

        self.runtime_mock.get_block.return_value = target_stage

        with mock.patch.object(self.block, 'get_workgroups_and_students') as patched_dashboard_params:
            patched_dashboard_params.return_value = (['irrelevant'], all_users)

            response = self.block.download_incomplete_list(request_mock)

        self.assertEqual(response.status_code, 200)
        # pylint: disable=unnecessary-comprehension
        reader = csv.DictReader([line for line in response.text.split("\n")])
        lines = [line for line in reader]
        self.assertEqual(reader.fieldnames, GroupProjectXBlock.CSV_HEADERS)
        self.assertEqual(lines[0], csv_repr(all_users[1]))
        self.assertEqual(lines[1], csv_repr(all_users[2]))
    def test_download_incomplete_list(self, all_users_ids, completed_users_ids, users_to_export_ids):
        request_mock = mock.Mock()
        request_mock.GET = {Constants.ACTIVATE_BLOCK_ID_PARAMETER_NAME: 'target_stage_id'}

        target_stage = mock.Mock(spec=BaseGroupActivityStage)
        target_stage.display_name = 'Stage 1'
        target_stage.get_users_completion.return_value = (set(completed_users_ids), {'irrelevant'})

        self.runtime_mock.get_block.return_value = target_stage

        # pylint: disable=maybe-no-member
        expected_filename = GroupProjectXBlock.REPORT_FILENAME.format(
            group_project_name=self.block.display_name, stage_name=target_stage.display_name,
            timestamp=datetime.utcnow().strftime(GroupProjectXBlock.CSV_TIMESTAMP_FORMAT)
        )
        with mock.patch.object(self.block, 'export_users', mock.Mock(wraps=self.block.export_users)) as export_users, \
                mock.patch.object(self.block, 'get_workgroups_and_students') as patched_dashboard_params:
            patched_dashboard_params.return_value = (
                ['irrelevant'],
                [ReducedUserDetails(id=uid, first_name="irrelevant", last_name="irrelevant") for uid in all_users_ids]
            )

            response = self.block.download_incomplete_list(request_mock)
            actual_parameters = export_users.call_args_list

        self.assertEqual(response.status_code, 200)
        self.runtime_mock.get_block.assert_called_with('target_stage_id')
        self.assertEqual(len(actual_parameters), 1)
        args, kwargs = actual_parameters[0]
        self.assertEqual(len(args), 2)
        self.assertEqual(kwargs, {})
        self.assertEqual({user.id for user in args[0]}, set(users_to_export_ids))
        self.assertEqual(args[1], expected_filename)
def make_reduced_user_details(**kwargs):
    defaults = {
        'id': 1,
        'username': '******',
        'email': '*****@*****.**',
        'first_name': 'test',
        'last_name': 'user'
    }
    defaults.update(kwargs)
    return ReducedUserDetails(**kwargs)
Esempio n. 4
0
    def test_review_status(self, peers_to_review, questions, reviews, expected_result):
        self.project_api_mock.get_peer_review_items_for_group.return_value = reviews

        with patch_obj(self.block_to_test, 'review_subjects', mock.PropertyMock()) as patched_review_subjects, \
                patch_obj(self.block_to_test, 'required_questions', mock.PropertyMock()) as patched_questions:
            patched_review_subjects.return_value = [ReducedUserDetails(id=rev_id) for rev_id in peers_to_review]
            patched_questions.return_value = [make_question(q_id, 'irrelevant') for q_id in questions]

            self.assertEqual(self.block.review_status(), expected_result)
            self.project_api_mock.get_peer_review_items_for_group.assert_called_once_with(
                self.workgroup_data.id, self.activity_mock.content_id
            )
Esempio n. 5
0
    def assert_users_completion(self, expected_result, questions, target_users, workgroups=None):
        target_workgroups = workgroups if workgroups else ['irrelevant']

        expected_completed, expected_partially_completed = expected_result
        with patch_obj(self.block_to_test, 'required_questions', mock.PropertyMock()) as patched_questions:
            patched_questions.return_value = [make_question(q_id, 'irrelevant') for q_id in questions]

            completed, partially_completed = self.block.get_users_completion(
                target_workgroups,
                [ReducedUserDetails(id=uid) for uid in target_users]
            )
        self.assertEqual(completed, expected_completed)
        self.assertEqual(partially_completed, expected_partially_completed)