master
ahmetkotan 2019-02-02 02:20:25 +03:00
rodzic cc1e9c4052
commit 75eaeee9cc
2 zmienionych plików z 234 dodań i 0 usunięć

Wyświetl plik

@ -1,3 +1,108 @@
from django.test import TestCase
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AnonymousUser
from rest_framework.test import APIClient, APIRequestFactory
from tokenauth.api_settings import api_settings
from picontrol.control import write_pin_mode, write_pin_value
from pins.views import PinView
# Create your tests here.
class PinApiTest(TestCase):
PIN = 11
TEST_USERNAME = "test_user"
TEST_PASSWORD = "test_password"
OUT_MODE = 0
IN_MODE = 1
HIGH_VALUE = 1
LOW_VALUE = 0
def setUp(self):
User = get_user_model()
self.test_user = User.objects.create(username=self.TEST_USERNAME, is_active=True)
self.test_user.set_password(self.TEST_PASSWORD)
self.test_user.save()
self.factory = APIRequestFactory()
self.api_client = APIClient()
response = self.api_client.post("/tokens/",
{"username": self.TEST_USERNAME, "password": self.TEST_PASSWORD},
format="json")
self.assertEqual(response.status_code, 200)
self.token = response.data.get('token')
self.api_client.credentials(HTTP_AUTHORIZATION=api_settings.TOKEN_PREFIX + " " + self.token)
def test_get_pin_permission(self):
url = "/pins/api/%s" % self.PIN
request = self.factory.get(url)
request.user = AnonymousUser()
response = PinView.as_view()(request, self.PIN)
self.assertEqual(response.status_code, 200)
def test_post_pin_permission(self):
url = "/pins/api/%s" % self.PIN
request = self.factory.post(url)
request.user = AnonymousUser()
response = PinView.as_view()(request, self.PIN)
self.assertEqual(response.status_code, 403)
request.user = self.test_user
response = PinView.as_view()(request, self.PIN)
self.assertEqual(response.status_code, 200)
def test_post_pin(self):
url = "/pins/api/%s" % self.PIN
data = self.api_client.post(url, {"mode": self.OUT_MODE}, format="json").data
self.assertTrue(data.get("operation"))
self.assertEqual(data.get("pin").get("mode"), self.OUT_MODE)
self.assertEqual(data.get("pin").get("hr_mode"), "OUT")
data = self.api_client.post(url, {"value": self.HIGH_VALUE}, format="json").data
self.assertTrue(data.get("operation"))
self.assertEqual(data.get("pin").get("value"), self.HIGH_VALUE)
self.assertEqual(data.get("pin").get("hr_value"), "HIGH")
def test_get_pin(self):
url = "/pins/api/%s" % self.PIN
data = self.api_client.get(url).data
self.assertEqual(data.get("BCM"), 17)
def test_write_and_get_pin(self):
write_pin_mode(self.PIN, self.OUT_MODE)
write_pin_value(self.PIN, self.HIGH_VALUE)
url = "/pins/api/%s" % self.PIN
data = self.api_client.get(url).data
self.assertEqual(data.get("value"), self.HIGH_VALUE)
self.assertEqual(data.get("hr_value"), "HIGH")
self.assertEqual(data.get("mode"), self.OUT_MODE)
self.assertEqual(data.get("hr_mode"), "OUT")

Wyświetl plik

@ -0,0 +1,129 @@
{
"variables": [],
"info": {
"name": "RestPi",
"_postman_id": "69afd048-0128-b0a9-3c82-54fe9ae7bc74",
"description": "",
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
},
"item": [
{
"name": "RestPi Login",
"event": [
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
"var responseJSON;",
"try {",
" responseJSON = JSON.parse(responseBody); ",
"}catch(e){",
" console.log(\"Error.\");",
"}",
"postman.setEnvironmentVariable(\"token\", responseJSON.token);",
"tests[\"Login successful\"] = responseCode.code === 200;"
]
}
}
],
"request": {
"url": "http://192.168.1.29:8001/tokens/",
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"description": ""
}
],
"body": {
"mode": "raw",
"raw": "{\n\t\"username\":\"{{username}}\",\n\t\"password\":\"{{password}}\"\n}"
},
"description": ""
},
"response": []
},
{
"name": "Change Pin Mode and Value",
"event": [
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
"var responseJSON;",
"try {",
" responseJSON = JSON.parse(responseBody); ",
"}catch(e){",
" console.log(\"Error.\");",
"}",
"",
"tests[\"Post pin is ok\"] = responseCode.code === 200;",
"tests[\"Mode and value changed\"] = responseJSON.operation === true;"
]
}
}
],
"request": {
"url": "http://192.168.1.29:8001/pins/api/11",
"method": "POST",
"header": [
{
"key": "Authorization",
"value": "Bearer {{token}}",
"description": ""
},
{
"key": "Content-Type",
"value": "application/json",
"description": ""
}
],
"body": {
"mode": "raw",
"raw": "{\n\t\"mode\": 0,\n\t\"value\": 1\n}"
},
"description": ""
},
"response": []
},
{
"name": "Get Pin",
"event": [
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
"var responseJSON;",
"try {",
" responseJSON = JSON.parse(responseBody); ",
"}catch(e){",
" console.log(\"Error.\");",
"}",
"",
"tests[\"Get pin is ok\"] = responseCode.code === 200;",
"tests[\"Pin values is ok\"] = responseJSON.mode === 0 && responseJSON.value === 1;"
]
}
}
],
"request": {
"url": "http://192.168.1.29:8001/pins/api/11",
"method": "GET",
"header": [
{
"key": "Authorization",
"value": "Bearer {{token}}",
"description": ""
}
],
"body": {},
"description": ""
},
"response": []
}
]
}