-
-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2285 from kobotoolbox/1804-per-user-settings
Add model for per-user-settings
- Loading branch information
Showing
7 changed files
with
134 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
import jsonbfield.fields | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('hub', '0004_configurationfile'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='PerUserSetting', | ||
fields=[ | ||
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), | ||
('user_queries', jsonbfield.fields.JSONField(help_text='A JSON representation of a *list* of Django queries, e.g. `[{"email__endswith": "@kobotoolbox.org"}, {"email__endswith": "@kbtdev.org"}]`. A matching user is one who would be returned by ANY of the queries in the list.')), | ||
('name', models.CharField(unique=True, max_length=255)), | ||
('value_when_matched', models.CharField(max_length=2048, blank=True)), | ||
('value_when_not_matched', models.CharField(max_length=2048, blank=True)), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from django.contrib.auth.models import User | ||
from django.core.urlresolvers import reverse | ||
from django.test import TestCase | ||
|
||
from hub.models import PerUserSetting | ||
|
||
|
||
class PerUserSettingTestCase(TestCase): | ||
|
||
def setUp(self): | ||
self.user_for_username_match = User.objects.create( | ||
username = 'match_me') | ||
self.user_for_email_match = User.objects.create( | ||
username='no_match_here', | ||
email = '[email protected]', | ||
) | ||
self.non_matching_user = User.objects.create(username='leave_me_alone') | ||
self.setting = PerUserSetting.objects.create( | ||
name='test', | ||
user_queries=[{"username__icontains": "Match"}, | ||
{"email__iendswith": "MatchThis.int"}], | ||
value_when_matched='great!', | ||
value_when_not_matched='okay...', | ||
) | ||
|
||
def test_matching_user(self): | ||
for u in [self.user_for_username_match, self.user_for_email_match]: | ||
self.assertTrue(self.setting.user_matches(u)) | ||
self.assertEqual(self.setting.get_for_user(u), 'great!') | ||
|
||
def test_non_matching_user(self): | ||
u = self.non_matching_user | ||
self.assertFalse(self.setting.user_matches(u)) | ||
self.assertEqual(self.setting.get_for_user(u), 'okay...') | ||
|
||
|
||
class IntercomConfigurationTestCase(TestCase): | ||
fixtures = ['test_data'] | ||
|
||
def setUp(self): | ||
self.setting = PerUserSetting.objects.create( | ||
name='INTERCOM_APP_ID', | ||
user_queries=[{"username": "someuser"}], | ||
value_when_matched='arm&leg', | ||
value_when_not_matched='', | ||
) | ||
|
||
def test_intercom_for_matching_user(self): | ||
self.assertTrue(self.client.login(username='someuser', | ||
password='someuser')) | ||
response = self.client.get(reverse('kpi-root')) | ||
lines = [line.strip() for line in response.content.split('\n')] | ||
self.assertTrue("window.IntercomAppId = 'arm&leg';" in lines) | ||
|
||
def test_no_intercom_for_non_matching_user(self): | ||
self.assertTrue(self.client.login(username='anotheruser', | ||
password='anotheruser')) | ||
response = self.client.get(reverse('kpi-root')) | ||
lines = [line.strip() for line in response.content.split('\n')] | ||
self.assertFalse("window.IntercomAppId = 'arm&leg';" in lines) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters