Debian Codegraph @

tests/test_projector.py

raw
1 # -*- coding: utf-8 -*-
2
3 from __future__ import unicode_literals
4
5 import hashlib
6 import unittest
7
8 from pypjlink.projector import Projector, POWER_STATES
9 from .utils import MockProjSocket
10
11
12 NO_AUTH_RESPONSE = 'PJLINK 0\r'
13
14
15 class AuthenticationTC(unittest.TestCase):
16 def test_no_auth(self):
17 """test detection of no auth projector"""
18 power_response = '%1POWR=1\r'
19 with MockProjSocket(NO_AUTH_RESPONSE + power_response) as mock_stream:
20
21 proj = Projector.from_address('projhost')
22 self.assertFalse(proj.authenticate(lambda: ''))
23 # since projector said no auth required, I shouldn't have written
24 # anything to it
25 self.assertFalse(mock_stream.write.called)
26
27 def test_auth(self):
28 """test authentication"""
29 auth_response = 'PJLINK 1 00112233\r'
30 power_response = '%1POWR=1\r'
31
32 with MockProjSocket(auth_response + power_response) as mock_stream:
33 proj = Projector.from_address('projhost')
34 self.assertTrue(proj.authenticate(lambda: 'p'))
35 # test write of authentication
36 self.assertEqual(
37 mock_stream.written,
38 self._md5_auth_code('00112233', 'p') + '%1POWR ?\r')
39
40 def test_wrong_auth(self):
41 """test failed authentication"""
42 response = (
43 'PJLINK 1 00112233\r'
44 'PJLINK ERRA\r'
45 )
46
47 with MockProjSocket(response) as mock_stream:
48 proj = Projector.from_address('projhost')
49 self.assertFalse(proj.authenticate(lambda: 'p'))
50 self.assertEqual(
51 mock_stream.written,
52 self._md5_auth_code('00112233', 'p') + '%1POWR ?\r')
53
54 def test_string_password(self):
55 """since 1.1.1, password can be a string in authenticate() method"""
56 auth_response = 'PJLINK 1 00112244\r'
57 power_response = '%1POWR=1\r'
58 with MockProjSocket(auth_response + power_response) as mock_stream:
59 proj = Projector.from_address('projhost')
60 self.assertTrue(proj.authenticate('ps'))
61 # test write of authentication
62 self.assertEqual(
63 mock_stream.written,
64 self._md5_auth_code('00112244', 'ps') + '%1POWR ?\r')
65
66 def test_no_password_no_auth(self):
67 """since 1.1.1, password can be omitted from authenticate() method
68 if projector doesn't need a password"""
69 power_response = '%1POWR=1\r'
70 with MockProjSocket(NO_AUTH_RESPONSE + power_response) as mock_stream:
71 proj = Projector.from_address('projhost')
72 self.assertFalse(proj.authenticate())
73 self.assertFalse(mock_stream.write.called)
74
75 def test_no_password_auth_required(self):
76 """if projector needs a password but is missing in authenticate() there
77 should be an error"""
78 auth_response = 'PJLINK 1 00112255\r'
79 power_response = '%1POWR=1\r'
80 with MockProjSocket(auth_response + power_response) as mock_stream:
81 proj = Projector.from_address('projhost')
82 with self.assertRaises(RuntimeError):
83 proj.authenticate()
84 self.assertFalse(mock_stream.write.called)
85
86 def _md5_auth_code(self, salt, password):
87 return hashlib.md5((salt + password).encode('utf-8')).hexdigest()
88
89
90 class NameTC(unittest.TestCase):
91 def test_unicode(self):
92 """test utf-8 chars in projector name"""
93 response = NO_AUTH_RESPONSE + '%1NAME=à€\r'
94 with MockProjSocket(response) as mock_stream:
95 proj = Projector.from_address('projhost')
96 proj.authenticate(lambda: '')
97 name = proj.get_name()
98 self.assertEqual(name, 'à€')
99
100
101 class PowerTC(unittest.TestCase):
102 def test_get(self):
103 response = NO_AUTH_RESPONSE + '%1POWR={}\r'.format(
104 POWER_STATES['cooling'])
105 with MockProjSocket(response) as mock_stream:
106 proj = Projector.from_address('projhost')
107 proj.authenticate(lambda: '')
108 self.assertEqual(proj.get_power(), 'cooling')
109
110 def test_set(self):
111 response = NO_AUTH_RESPONSE + '%1POWR=OK\r'
112 with MockProjSocket(response) as mock_stream:
113 proj = Projector.from_address('projhost')
114 proj.authenticate(lambda: '')
115 proj.set_power('off')
116
117 self.assertEqual(
118 mock_stream.written, '%1POWR {}\r'.format(POWER_STATES['off']))