|
1 """ |
|
2 Tests the core api for sessions |
|
3 """ |
|
4 import logging |
|
5 |
|
6 from django.contrib.auth import get_user_model |
|
7 from django.contrib.auth.models import Group |
|
8 from django.urls import reverse |
|
9 from django.utils import timezone |
|
10 from notes import constants |
|
11 from notes.api.fields import ProtocolField |
|
12 from rest_framework import serializers, status |
|
13 from rest_framework.test import APITransactionTestCase |
|
14 |
|
15 logger = logging.getLogger(__name__) |
|
16 |
|
17 class ProtocolFieldTests(APITransactionTestCase): |
|
18 |
|
19 def setUp(self): |
|
20 User = get_user_model() |
|
21 self.user1 = User.objects.create_user( |
|
22 username='test_user1', |
|
23 email='test_user@emial.com', |
|
24 password='top_secret' |
|
25 ) |
|
26 |
|
27 self.group = Group(name='group1') |
|
28 self.group.save() |
|
29 self.group.profile.owner = self.user1 |
|
30 self.group.profile.save() |
|
31 |
|
32 |
|
33 def test_to_representation(self): |
|
34 class GroupSerializer(serializers.Serializer): |
|
35 protocol = ProtocolField(source='profile.protocol') |
|
36 |
|
37 serializer = GroupSerializer(self.group) |
|
38 data = serializer.data |
|
39 |
|
40 logger.debug("serializer data : %r", repr(data)) |
|
41 |
|
42 self.assertIn('protocol', data) |
|
43 protocol_data = data.get('protocol') |
|
44 |
|
45 self.assertIn('id', protocol_data) |
|
46 self.assertEqual(self.group.profile.protocol, "%s%s.%s" % (constants.PROTOCOL_URN_PREFIX,protocol_data['id'], protocol_data['version'])) |
|
47 |
|
48 def test_to_internal_value(self): |
|
49 protocol_field = ProtocolField() |
|
50 |
|
51 protocol_id, protocol_version = self.group.profile.protocol[len(constants.PROTOCOL_URN_PREFIX):].split('.') |
|
52 |
|
53 data = { |
|
54 'id': protocol_id, |
|
55 'owner': 'group1', |
|
56 'title': 'Protocol group group1', |
|
57 'description': 'Protocol group group1', |
|
58 'version': protocol_version, |
|
59 'metacategories': [] |
|
60 } |
|
61 |
|
62 value = protocol_field.to_internal_value(data) |
|
63 |
|
64 self.assertEqual(value, self.group.profile.protocol) |