import json,uuid from datetime import timedelta from django.test import TestCase from django.contrib.auth.models import User from django.utils import timezone from .models import Device,DeviceIdentity,ChoiceValue,InventoryAPIKey,InspectionSubmission,InspectionRecord class StationTests(TestCase): def setUp(self): self.user=User.objects.create_user('station',is_superuser=True) _,self.key=InventoryAPIKey.objects.create_key(name='station',service_user=self.user,can_read=True,can_write=True,expiry_date=timezone.now()+timedelta(days=1)) state=ChoiceValue.objects.create(kind='status',label='Eingang') ChoiceValue.objects.get_or_create(kind='status',label='Inspektion',defaults={'active':True}) kind=ChoiceValue.objects.create(kind='device_type',label='Laptop') self.d=Device.objects.create(status=state,device_type=kind,model='Test',ram_gb=8) self.hwid='dmi-uuid:'+str(uuid.uuid4()) DeviceIdentity.objects.create(hwid=self.hwid,device=self.d) def post(self,route,body,key=None): return self.client.post(f'/api/v1/devices/{self.d.number}/{route}/',json.dumps(body),content_type='application/json',HTTP_AUTHORIZATION='Api-Key '+(key or self.key)) def inspection(self,phase='STARTED'): self.d.refresh_from_db() return {'hwid':self.hwid,'expected_revision':self.d.revision,'request_id':str(uuid.uuid4()),'phase':phase,'technician':'Tech','notes':'','results':{'wifi':{'outcome':'PASS','notes':'Connected','history':[]}}} def test_intake_specs_then_start_changes_stage_and_status(self): r=self.post('intake',{'hwid':self.hwid,'expected_revision':self.d.revision,'specifications':{'ram_gb':16,'ssd_gb':512}}) self.assertEqual(r.status_code,200,r.content);self.assertEqual(r.json()['stage'],'INTAKE') r=self.post('inspection',self.inspection());self.assertEqual(r.status_code,201,r.content) self.assertEqual(r.json()['stage'],'INSPECTION');self.assertEqual(r.json()['status']['label'],'Inspektion') self.assertEqual(InspectionSubmission.objects.count(),1) def test_repeat_and_completion_preserve_structured_results(self): body=self.inspection('COMPLETE');body['results']['wifi']['outcome']='UNAVAILABLE' r=self.post('inspection',body);self.assertEqual(r.status_code,201,r.content) self.assertEqual(self.post('inspection',body).status_code,200) self.assertEqual(InspectionSubmission.objects.count(),1);self.assertEqual(InspectionRecord.objects.count(),1) self.assertEqual(InspectionSubmission.objects.get().payload['results']['wifi']['outcome'],'UNAVAILABLE') body['notes']='different';self.assertEqual(self.post('inspection',body).status_code,409) def test_incomplete_cannot_be_completed(self): body=self.inspection('COMPLETE');body['results']['wifi']['outcome']='NOT_TESTED' self.assertEqual(self.post('inspection',body).status_code,400) self.assertFalse(InspectionSubmission.objects.exists()) def test_stale_revision_wrong_device_and_later_stages_blocked(self): body=self.inspection();self.d.comment='Changed';self.d.save() self.assertEqual(self.post('inspection',body).status_code,409) body=self.inspection();body['hwid']='dmi-uuid:'+str(uuid.uuid4()) self.assertEqual(self.post('inspection',body).status_code,400) self.d.stage='STORAGE';self.d.save() self.assertEqual(self.post('inspection',self.inspection()).status_code,409) self.assertEqual(self.post('intake',{'hwid':self.hwid,'expected_revision':self.d.revision,'specifications':{'ram_gb':32}}).status_code,409) self.d.refresh_from_db();self.assertEqual(self.d.ram_gb,8) def test_readonly_cannot_change_inspection(self): _,key=InventoryAPIKey.objects.create_key(name='read',service_user=self.user,can_read=True,can_write=False,expiry_date=timezone.now()+timedelta(days=1)) self.assertEqual(self.post('inspection',self.inspection(),key).status_code,403)