Exemple #1
0
 def test_delete_rule(self, client, org_name, status_code, expected_post_delete_count):
     """
     Ensure rule deletion is possible via the API.
     """
     org = Organization.objects.get(name=org_name)
     rule = MembershipRuleFactory.create(group__organization=org)
     response = client.delete('/membership-rules/{}/'.format(rule.id))
     assert response.status_code == status_code, response.content
     assert MembershipRule.objects.count() == expected_post_delete_count
Exemple #2
0
 def test_rule(self, client, org_name, status_code, expected_name):
     org = Organization.objects.get(name=org_name)
     rule = MembershipRuleFactory.create(group__organization=org, name='old name', domain='w3c.org')
     assert rule.name == 'old name'
     response = client.patch('/membership-rules/{}/'.format(rule.id), {
         'name': 'Community group',
         'domain': 'example.org',
     }, content_type='application/json')
     assert response.status_code == status_code, response.content
     new_rule = MembershipRule.objects.get()
     assert new_rule.name == expected_name
Exemple #3
0
def test_failed_membership_rule_signals(monkeypatch, caplog, receiver_function, signal_name):
    """
    Ensure  errors in USER_ACCOUNT_ACTIVATED and REGISTER_USER are logged.
    """
    monkeypatch.delattr(Membership, 'create_from_rules')  # Act as if create_from_rules() don't work!

    user = UserFactory.create(email='*****@*****.**')
    MembershipRuleFactory(domain='example.com')

    with pytest.raises(AttributeError):
        receiver_function(object(), user)
    assert 'Error receiving {signal_name} signal for user'.format(signal_name=signal_name) in caplog.text
    assert '*****@*****.**' in caplog.text
    assert 'AttributeError' in caplog.text
Exemple #4
0
 def test_one_rule(self, client, org_name, status_code, skip_response_check):
     org = Organization.objects.get(name=org_name)
     rule = MembershipRuleFactory.create(group__organization=org)
     response = client.get('/membership-rules/{}/'.format(rule.id))
     result = response.json()
     assert response.status_code == status_code, response.content
     assert skip_response_check or (result == {
         'id': rule.id,
         'name': rule.name,
         'domain': rule.domain,
         'group': {
             'id': rule.group.id,
             'name': rule.group.name,
         },
     }), 'Verify the serializer results.'
Exemple #5
0
def test_working_membership_rule_signals(receiver_function):
    """
    Ensure USER_ACCOUNT_ACTIVATED and REGISTER_USER signals are processed correctly.
    """
    rule = MembershipRuleFactory(domain='example.com')
    mapping = UserOrganizationMappingFactory.create(
        user__email='*****@*****.**',
        user__is_active=True,
        organization=rule.group.organization,
    )

    receiver_function(object(), mapping.user)
    assert Membership.objects.filter(user=mapping.user).exists(), 'Should create the rule'

    receiver_function(object(), mapping.user)  # Should not fail when receiving the signal twice
Exemple #6
0
def test_register_user_signal_inactive_user(caplog):
    """
    Ensure REGISTER_USER signal is not processed for inactive users.

    Otherwise, `Membership.create_from_rules` would raise an exception.
    """
    caplog.set_level(logging.INFO)  # Ensure INFO logs are captured
    rule = MembershipRuleFactory(domain='example.com')
    mapping = UserOrganizationMappingFactory.create(
        user__email='*****@*****.**',
        user__is_active=False,
        organization=rule.group.organization,
    )

    on_learner_register(object(), mapping.user)
    assert not Membership.objects.filter(user=mapping.user).exists(), 'Should not create the rule for inactive user'
    assert 'Received REGISTER_USER signal for inactive user' in caplog.text
Exemple #7
0
 def test_list_rules(self, client, org_name, status_code, expected_count):
     org = Organization.objects.get(name=org_name)
     MembershipRuleFactory.create_batch(3, group__organization=org)
     response = client.get(self.url)
     assert response.status_code == status_code, response.content
     assert response.json()['count'] == expected_count
 def test_invalid_domains(self, domain):
     with pytest.raises(ValidationError):
         rule = MembershipRuleFactory.create(domain=domain)
         rule.full_clean()
 def test_valid_domains(self, domain):
     rule = MembershipRuleFactory.create(domain=domain)
     rule.full_clean()
     assert rule