forked from google/makerspace-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_two_button.py
More file actions
81 lines (71 loc) · 2.56 KB
/
Copy pathtest_two_button.py
File metadata and controls
81 lines (71 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Copyright 2018 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for two_button.py"""
import sys
import unittest
import tempfile
import threading
import time
import two_button
import authbox.api
import authbox.badgereader_hid_keystroking
from authbox import fake_gpio_for_testing
from authbox import fake_evdev_device_for_testing
# Assumed to be the fake one, by authbox/__init__.py magic.
from RPi import GPIO
SAMPLE_CONFIG = b'''
[pins]
on_button=Button:1:2
off_button=Button:3:4
enable_output=Relay:ActiveHigh:5
badge_reader=HIDKeystrokingReader:badge_scanner
buzzer=Buzzer:9
[auth]
duration=20s
warning=10s
extend=20s
command = touch enabled
extend_command = touch enabled
deauth_command = rm -f enabled
'''
# This is the fastest way to ensure that basic logic is right, but it does not
# test the use of BaseDispatcher.event_queue or the way callbacks happen on the
# same thread serialized.
class SimpleDispatcherTest(unittest.TestCase):
def setUp(self):
authbox.badgereader_hid_keystroking.evdev.list_devices = fake_evdev_device_for_testing.list_devices
authbox.badgereader_hid_keystroking.evdev.InputDevice = fake_evdev_device_for_testing.InputDevice
self.gpio = fake_gpio_for_testing.FakeGPIO()
with tempfile.NamedTemporaryFile() as f:
f.write(SAMPLE_CONFIG)
f.flush()
config = authbox.config.Config(f.name)
self.dispatcher = two_button.Dispatcher(config)
def test_auth_flow(self):
# Out of the box, relay should be off
self.assertFalse(self.dispatcher.authorized)
self.assertFalse(GPIO.input(5))
# Badge scan sets authorized flag, but doesn't enable relay until button
# press.
self.dispatcher.badge_scan('1234')
self.assertTrue(self.dispatcher.authorized)
self.assertFalse(GPIO.input(5))
# "On" button pressed
self.dispatcher.on_button_down(None)
self.assertTrue(self.dispatcher.authorized)
self.assertTrue(GPIO.input(5))
# "Off" button pressed
self.dispatcher.abort(None)
self.assertFalse(self.dispatcher.authorized)
self.assertFalse(GPIO.input(5))