|
| 1 | +import unittest |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +import app as app_module |
| 5 | +from scanner import NFCStandbyReader |
| 6 | + |
| 7 | + |
| 8 | +class RegistrationFlowTests(unittest.TestCase): |
| 9 | + def test_reader_cache_becomes_registered_immediately(self): |
| 10 | + reader = NFCStandbyReader(on_tap=lambda _uid: {"message": "School ID detected"}) |
| 11 | + reader._last_uid = "CARD-1" |
| 12 | + reader._last_payload = None |
| 13 | + reader._cooldown["CARD-1"] = 1 |
| 14 | + |
| 15 | + updated = reader.cache_registered_user("CARD-1", { |
| 16 | + "firstname": "Alex", |
| 17 | + "fullname": "ALEX SANTOS", |
| 18 | + "student_no": "2026-00001", |
| 19 | + "course": "BS COMPUTER ENGINEERING", |
| 20 | + }) |
| 21 | + |
| 22 | + self.assertTrue(updated) |
| 23 | + self.assertEqual(reader.latest_tap(-1)["user"]["student_no"], "2026-00001") |
| 24 | + self.assertNotIn("CARD-1", reader._cooldown) |
| 25 | + |
| 26 | + @patch.object(app_module, "enqueue_sync") |
| 27 | + @patch.object(app_module.nfc_reader, "cache_registered_user") |
| 28 | + @patch.object(app_module, "create_user") |
| 29 | + @patch.object(app_module, "get_user_by_nfc", return_value=None) |
| 30 | + @patch.object(app_module, "valid_latest_tap", return_value={"uid": "CARD-1", "tap_counter": 7}) |
| 31 | + def test_registration_response_is_immediately_usable( |
| 32 | + self, |
| 33 | + _valid_tap, |
| 34 | + _get_user, |
| 35 | + create_user, |
| 36 | + cache_registered_user, |
| 37 | + enqueue_sync, |
| 38 | + ): |
| 39 | + create_user.return_value = { |
| 40 | + "id": 12, |
| 41 | + "firstname": "ALEX", |
| 42 | + "fullname": "ALEX SANTOS", |
| 43 | + "student_no": "2026-00001", |
| 44 | + "course": "BS COMPUTER ENGINEERING", |
| 45 | + "nfc_code": "CARD-1", |
| 46 | + } |
| 47 | + client = app_module.app.test_client() |
| 48 | + |
| 49 | + response = client.post("/register_from_tap", json={ |
| 50 | + "uid": "CARD-1", |
| 51 | + "tap_counter": 7, |
| 52 | + "firstname": "Alex", |
| 53 | + "lastname": "Santos", |
| 54 | + "student_no": "2026-00001", |
| 55 | + "course": "BS Computer Engineering", |
| 56 | + }) |
| 57 | + |
| 58 | + self.assertEqual(response.status_code, 200) |
| 59 | + self.assertTrue(response.get_json()["success"]) |
| 60 | + self.assertEqual(response.get_json()["user"]["student_no"], "2026-00001") |
| 61 | + cache_registered_user.assert_called_once() |
| 62 | + enqueue_sync.assert_called_once() |
| 63 | + |
| 64 | + @patch.object(app_module, "is_user_checked_in", return_value=False) |
| 65 | + @patch.object(app_module.nfc_reader, "cache_registered_user") |
| 66 | + @patch.object(app_module, "get_user_by_nfc") |
| 67 | + @patch.object(app_module, "valid_latest_tap", return_value={"uid": "CARD-1", "tap_counter": 8}) |
| 68 | + def test_registration_retry_recovers_existing_card( |
| 69 | + self, |
| 70 | + _valid_tap, |
| 71 | + get_user, |
| 72 | + cache_registered_user, |
| 73 | + _checked_in, |
| 74 | + ): |
| 75 | + get_user.return_value = { |
| 76 | + "id": 12, |
| 77 | + "firstname": "ALEX", |
| 78 | + "fullname": "ALEX SANTOS", |
| 79 | + "student_no": "2026-00001", |
| 80 | + "course": "BS COMPUTER ENGINEERING", |
| 81 | + "nfc_code": "CARD-1", |
| 82 | + } |
| 83 | + client = app_module.app.test_client() |
| 84 | + |
| 85 | + response = client.post("/register_from_tap", json={ |
| 86 | + "uid": "CARD-1", |
| 87 | + "tap_counter": 8, |
| 88 | + "firstname": "Alex", |
| 89 | + "lastname": "Santos", |
| 90 | + "student_no": "2026-00001", |
| 91 | + "course": "BS Computer Engineering", |
| 92 | + }) |
| 93 | + |
| 94 | + self.assertEqual(response.status_code, 200) |
| 95 | + self.assertEqual(response.get_json()["message"], "This card is already registered.") |
| 96 | + cache_registered_user.assert_called_once() |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + unittest.main() |
0 commit comments