class TestLinkUser(unittest.TestCase):
    def setUp(self):
        self.new_user = User(first_name="First",
                             last_name="Last",
                             username="******")
        self.new_user.save()
        from datawinners.accountmanagement.models import Organization
        org_id = Organization.objects.all()[0].org_id
        self.ngouser = NGOUserProfile(user=self.new_user,
                                      org_id=org_id,
                                      reporter_id="ds406")
        self.ngouser.save()

    @patch(
        'datawinners.accountmanagement.user_tasks.make_user_data_sender_with_project'
    )
    @patch('datawinners.accountmanagement.user_tasks.get_all_projects')
    @patch('datawinners.accountmanagement.user_tasks.get_database_manager')
    def test_should_link_user_as_datasender_to_all_project(
            self, get_database_manager_mock, get_all_projects_mock,
            make_user_data_sender_with_project_mock):
        all_projects = [{
            'value': {
                'datasenders': ["ds406", "rep011"],
                "_id": "27348932184297"
            }
        }]
        get_database_manager_mock.return_value = "dbm"
        get_all_projects_mock.return_value = all_projects
        link_user_to_all_projects.apply(args=(self.new_user.id, )).get()
        get_database_manager_mock.assert_called_with(self.new_user)
        make_user_data_sender_with_project_mock.assert_called_with(
            "dbm", "ds406", "27348932184297")

    def tearDown(self):
        self.ngouser.delete()
        self.new_user.delete()
class TestPostActivationEvents(unittest.TestCase):
    def setUp(self):
        self.username = '******'
        self.paid_user, _ = User.objects.get_or_create(username=self.username,
                                                       email=self.username,
                                                       password='******')
        self.paid_user.save()
        self.paid_org = Organization(name='test_org_for_correct_active_date',
                                     sector='PublicHealth',
                                     address='add',
                                     city='city',
                                     country=Country('MG'),
                                     zipcode='10000',
                                     active_date=None)
        self.paid_org.save()
        self.mobile_number = "1233"
        self.paid_user_profile = NGOUserProfile(
            user=self.paid_user,
            title='Mr.',
            org_id=self.paid_org.org_id,
            mobile_phone=self.mobile_number)
        self.paid_user_profile.save()

        self.trial_username = '******'
        self.trial_user, _ = User.objects.get_or_create(
            username=self.trial_username,
            email=self.trial_username,
            password='******')
        self.trial_user.save()

        self.trial_org = Organization(name='trial_org',
                                      sector='PublicHealth',
                                      address='add',
                                      city='city',
                                      country=Country('MG'),
                                      zipcode='10000',
                                      active_date=None,
                                      account_type='Basic',
                                      org_id='test')
        self.trial_org.save()

        self.trial_mobile_number = "12445"
        self.trial_user_profile = NGOUserProfile(
            user=self.trial_user,
            title='Mr.',
            org_id=self.trial_org.org_id,
            mobile_phone=self.trial_mobile_number)
        self.trial_user_profile.save()

        self.patcher1 = patch(
            'datawinners.accountmanagement.post_activation_events.get_entity_count_for_type'
        )
        self.get_all_entities_mock = self.patcher1.start()

        self.patcher2 = patch(
            'datawinners.accountmanagement.post_activation_events.create_contact'
        )
        self.create_entity_mock = self.patcher2.start()

    def test_active_organization_with_should_active_date_is_none_save_active_date(
            self):
        active_organization(org=self.paid_org)
        self.assertIsNotNone(
            Organization.objects.get(org_id=self.paid_org.org_id).active_date)
        self.assertEqual(
            Organization.objects.get(
                org_id=self.paid_org.org_id).active_date.microsecond, 0)

    def test_should_make_datasender_entity_for_paid_account(self):
        mock_manager = Mock(spec=DatabaseManager)
        entity_mock = Mock(spec=Contact)
        entity_mock.type_path = [REPORTER]
        self.get_all_entities_mock.return_value = 1
        self.create_entity_mock = entity_mock

        make_user_as_a_datasender(mock_manager, self.paid_org, self.username,
                                  self.mobile_number)

        entity_mock.add_data.assert_called_once()

    def test_should_make_datasender_entity_and_datasender_on_trial_account_for_trial_account(
            self):
        mock_manager = Mock(spec=DatabaseManager)
        entity_mock = Mock(spec=Entity)
        entity_mock.type_path = [REPORTER]
        self.get_all_entities_mock.return_value = 1
        self.create_entity_mock = entity_mock

        make_user_as_a_datasender(mock_manager, self.trial_org,
                                  self.trial_username,
                                  self.trial_mobile_number)

        entity_mock.add_data.assert_called_once()

        data_senders_on_trial_account = DataSenderOnTrialAccount.objects.filter(
            organization=self.trial_org)
        self.assertEqual(1, len(data_senders_on_trial_account))
        self.assertEqual(self.trial_mobile_number,
                         data_senders_on_trial_account[0].mobile_number)
        self.assertEqual(self.trial_org,
                         data_senders_on_trial_account[0].organization)

    def tearDown(self):
        self.paid_user_profile.delete()
        self.paid_org.delete()
        self.paid_user.delete()

        self.trial_user_profile.delete()
        self.trial_org.delete()
        self.trial_user.delete()
        DataSenderOnTrialAccount.objects.filter(
            organization=self.trial_org).delete()

        self.patcher1.stop()
        self.patcher2.stop()