コード例 #1
0
ファイル: test.py プロジェクト: azdkj532/ntu-vote-auth-server
    def setUpTestData(self):
        self.student_id = 'B03705024'

        self.username = '******'
        self.password = '******'
        self.user = User(username=self.username)
        self.user.set_password(self.password)
        self.user.kind = User.STATION
        self.user.save()

        self.name = 'NTU'
        self.station = Station()
        self.station.name = self.name
        self.station.user = self.user
        self.station.external_id = 1
        self.station.max_sessions = 3
        self.station.save()

        # login
        data = {'username': self.username, 'password': self.password,
                'api_key': settings.API_KEY, 'version': settings.API_VERSION}
        client = APIClient()
        client.post(reverse('register'), data, format='json')

        try:
            session = Session.objects.get(user=self.user)
        except:
            session = None
        self.session = session
        self.token = session.token

        self.authcode = AuthCode(kind='70', code='70-ZU2U0RAKX-KOXLUYHJI-7C05B')
        self.authcode.save()
コード例 #2
0
ファイル: test.py プロジェクト: azdkj532/ntu-vote-auth-server
class CoreTestCase(APITestCase):
    @classmethod
    def setUpTestData(self):
        self.student_id = 'B03705024'

        self.username = '******'
        self.password = '******'
        self.user = User(username=self.username)
        self.user.set_password(self.password)
        self.user.kind = User.STATION
        self.user.save()

        self.name = 'NTU'
        self.station = Station()
        self.station.name = self.name
        self.station.user = self.user
        self.station.external_id = 1
        self.station.max_sessions = 3
        self.station.save()

        # login
        data = {'username': self.username, 'password': self.password,
                'api_key': settings.API_KEY, 'version': settings.API_VERSION}
        client = APIClient()
        client.post(reverse('register'), data, format='json')

        try:
            session = Session.objects.get(user=self.user)
        except:
            session = None
        self.session = session
        self.token = session.token

        self.authcode = AuthCode(kind='70', code='70-ZU2U0RAKX-KOXLUYHJI-7C05B')
        self.authcode.save()

    @override_settings(
        ACA_API_URL='http://localhost:3000/seqServices/stuinfoByCardno',
        ACA_API_USER='******', ACA_API_PASSWORD='******',
        ENFORCE_CARD_VALIDATION=True)
    def test_authenticate_success(self):
        cid = '12345678'
        aca_info = service.to_student_id(cid)
        uid = aca_info.id
        uid = uid + '0'
        url = reverse('authenticate')
        data = {'cid': cid, 'uid': uid,
                'token': self.token,
                'api_key': settings.API_KEY, 'version': settings.API_VERSION}
        response = self.client.post(url, data)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        token = AuthToken.objects.get(student_id=aca_info.id)
        self.assertEqual(response.data, {
            'vote_token': token.code,
            'uid': aca_info.id,
            'type': aca_info.college,
            'status': 'success'
        })

    def test_confirm_success(self):
        record = Record(student_id=self.student_id)
        record.state = Record.LOCKED
        record.save()
        vote_token = AuthToken.generate(self.student_id, str(self.station.external_id), '70')
        vote_token.save()

        url = reverse('confirm')
        data = {'uid': self.student_id,
                'vote_token': vote_token.code, 'token': self.token,
                'api_key': settings.API_KEY, 'version': settings.API_VERSION}
        response = self.client.post(url, data)
        callback = 'https://{0}{1}?callback={2}'.format(
            settings.CALLBACK_DOMAIN, reverse('callback'), vote_token.confirm_code)
        self.assertEqual(response.data, {
            'status': 'success',
            'ballot': self.authcode.code,
            'callback': callback,
        })

    def test_complete_success(self):
        record = Record(student_id=self.student_id)
        record.state = Record.VOTING
        record.save()
        token = AuthToken.generate(self.student_id, str(self.station.external_id), '70')
        token.save()

        url = 'https://{0}{1}?callback={2}'.format(
            settings.CALLBACK_DOMAIN, reverse('callback'), token.confirm_code)
        response = self.client.get(url)
        record = Record.objects.get(student_id=self.student_id)
        self.assertEqual(record.state, Record.USED)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data, {
            'status': 'success',
            'message': 'all correct',
        })

    def test_report_success(self):
        record = Record(student_id=self.student_id)
        record.state = Record.LOCKED
        record.save()
        vote_token = AuthToken.generate(self.student_id, str(self.station.external_id), '70')
        vote_token.save()

        url = reverse('report')
        data = {
            'uid': self.student_id,
            'vote_token': vote_token.code,
            'token': self.token,
            'api_key': settings.API_KEY, 'version': settings.API_VERSION,
        }
        response = self.client.post(url, data)
        self.assertEqual(response.data, {'status': 'success'})

    def test_status(self):
        username = '******'
        password = '******'
        user = User(username=username)
        user.set_password(password)
        user.kind = User.ADMIN
        user.save()

        # login
        data = {'username': username, 'password': password,
                'api_key': settings.API_KEY, 'version': settings.API_VERSION}
        client = APIClient()
        client.post(reverse('register'), data, format='json')

        try:
            session = Session.objects.get(user=user)
        except:
            session = None
        else:
            url = '/api/status'
            data = {
                'token': session.token,
                'api_key': settings.API_KEY, 'version': settings.API_VERSION,
            }
            response = self.client.post(url, data)