def test_user_update_photo(admin_client): """ ... """ user_id: int = 1 origin_user = UserHeavySerializer(User.objects.get(id=user_id)).data file_path = (os.path.dirname(__file__)) + "/images/data_lab_480.png" # im = Image.open(file_path); im = open(file_path, "rb") # print(im) request = { "photo": im, } response = admin_client.post("/api/user/update_photo/", request, format="multipart") updated_user = UserHeavySerializer(User.objects.get(id=user_id)).data print(response.data) assert response.status_code == 200 # print(updated_user['photo']) assert origin_user != updated_user assert response.data == updated_user
def test_error_params_update_user(admin_client): """ ... """ oldvalues = UserHeavySerializer(User.objects.get(id=1)) newdata: Dict[str, Any] = { "usernames": "NEW", "first_namesss": "new name", } response = admin_client.put("/api/user/" + str(1) + "/", newdata) newvalues = UserHeavySerializer(User.objects.get(id=1)) assert response.status_code == 400 assert newvalues.data == oldvalues.data
def test_user_update_profile_invalid_params(admin_client): """ ... """ user_id: int = 1 oldvalues = UserHeavySerializer(User.objects.get(id=user_id)) newdata: Dict[str, Any] = { "usernames": "NEW", "first_names": "new name", } response = admin_client.post("/api/user/profile/", newdata) newvalues = UserHeavySerializer(User.objects.get(id=user_id)) assert response.status_code == 400 assert newvalues.data == oldvalues.data
def get(self, request, pk: Union[int, str], format=None): """ ... """ user = self.get_object(pk) response = UserHeavySerializer(user) return Response(response.data, status=status.HTTP_200_OK)
def test_update_user(admin_client): """ ... """ oldvalues = UserHeavySerializer(User.objects.get(id=1)) newdata: Dict[str, Any] = { "username": "******", "first_name": "new name", "last_name": "new name2", "email": "*****@*****.**", } response = admin_client.put("/api/user/" + str(1) + "/", newdata) newvalues = UserHeavySerializer(User.objects.get(id=1)) # assert response.data == 'yeah' assert response.status_code == 200 assert newvalues.data != oldvalues.data assert newvalues.data == response.data
def test_user_update_profile(admin_client): """ ... """ user_id: int = 1 oldvalues = UserHeavySerializer(User.objects.get(id=user_id)) newdata: Dict[str, Any] = { "username": "******", "first_name": "new name", "last_name": "new name2", "email": "*****@*****.**", } response = admin_client.post("/api/user/profile/", newdata) newvalues = UserHeavySerializer(User.objects.get(id=user_id)) assert response.status_code == 200 assert newvalues.data != oldvalues.data assert newvalues.data == response.data
def test_get_user(admin_client): """ ... """ response = admin_client.get("/api/user/" + str(1) + "/") serializer = UserHeavySerializer(User.objects.get(id=1)) assert response.status_code == 200 assert serializer.data == response.data
def get(self, request, *args, **kwargs): """ ... """ user_serializer = UserHeavySerializer(request.user) return Response( user_serializer.data, status=status.HTTP_200_OK, )
def test_get_all_user(admin_client): """ ... """ response = admin_client.get("/api/users/?page=1") serializer = UserHeavySerializer( User.objects.all()[:settings.REST_FRAMEWORK["PAGE_SIZE"]], many=True) assert response.status_code == 200 assert response.data["data"] == serializer.data assert response.data["total"] == User.objects.count()
def test_get_current_user_jwt(admin_client_jwt): """ admin_client_jwt -> user id: 1 """ user_serializer = UserHeavySerializer(User.objects.get(id=1)) response = admin_client_jwt.post("/api/current_user_jwt/") # assert response.data == 'si' assert response.status_code == 200 assert response.data == user_serializer.data
def post(self, request, format=None): """ ... """ response = self.serializer(request.user, data=request.data) if response.is_valid(): result = response.save() res = UserHeavySerializer(result) return Response(res.data, status=status.HTTP_200_OK) return Response(response.errors, status=status.HTTP_400_BAD_REQUEST)
def test_user_validate_update_photo(admin_client): """ ... """ user_id: int = 1 origin_user = UserHeavySerializer(User.objects.get(id=user_id)).data request = { "photo": False, } response = admin_client.post("/api/user/update_photo/", request, format="multipart") not_updated_user = UserHeavySerializer(User.objects.get(id=user_id)).data # print(response.data) assert response.status_code == 422 assert origin_user == not_updated_user
def put(self, request, pk: Union[int, str], format=None): """ ... """ user = self.get_object(pk) response = self.serializer(user, data=request.data) if response.is_valid(): result = response.save() res = UserHeavySerializer(result) return Response(res.data, status=status.HTTP_200_OK) return Response(response.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, *args, **kwargs): """ ... """ serializer = self.serializer_class( data=request.data, context={"request": request}, ) serializer.is_valid(raise_exception=True) user = serializer.validated_data["user"] user_serializer = UserHeavySerializer(user) token, _ = Token.objects.get_or_create(user=user) return Response( {"token": token.key, "user": user_serializer.data}, status=status.HTTP_200_OK, )
def post(self, request, format=None): """ user modifies his current photo """ response = self.serializer(request.user, data=request.data) if response.is_valid(): # print(response.validated_data); user = response.save() # print(user) serializer_user = UserHeavySerializer(user) return Response(serializer_user.data, status=status.HTTP_200_OK) # return Response(status=status.HTTP_200_OK) return Response(response.errors, status=status.HTTP_422_UNPROCESSABLE_ENTITY)
def test_create_user(admin_client): """ ... """ data: Dict[str, Any] = { "username": "******", "email": "*****@*****.**", "password": "******", "first_name": "name", "last_name": "name2", "is_staff": False, } response = admin_client.post("/api/users/", data) serializer = UserHeavySerializer(User.objects.get(id=response.data["id"]),) assert response.status_code == 201 assert serializer.data == response.data
def test_get_current_auth_user(admin_client): """[summary] [description] Decorators: pytest.mark.users_profile Arguments: admin_client {[type]} -- [description] """ user_serializer = UserHeavySerializer(User.objects.get(id=1)) response = admin_client.get("/api/user/profile/") # assert response.data == 'si' assert response.status_code == 200 assert response.data == user_serializer.data
def test_success_login_jwt(public_client): """ ... """ data: Dict[str, str] = { "email": "*****@*****.**", "password": "******", } response = public_client.post("/api/jwt-auth/", data) assert response.status_code == 200 decoded = jwt.decode(response.data, settings.KEY_HS256, algorithms="HS256") user = User.objects.get(email=data["email"]) serialer = UserHeavySerializer(user) token = Token.objects.get(user_id=user.id) assert decoded["token"] == token.key assert decoded["user"] == serialer.data
def admin_client_jwt(): """ user jwt authenticated """ user = User.objects.get(id=1) user_serializer = UserHeavySerializer(user) # print(user_serializer.data) token, _ = Token.objects.get_or_create(user=user) # print(token) key = settings.KEY_HS256 encoded_jwt = jwt.encode( {"token": token.key, "user": user_serializer.data}, key, algorithm="HS256" ) # print(type(encoded_jwt)) # print(encoded_jwt.decode('UTF-8')) client = APIClient() client.credentials(HTTP_AUTHORIZATION="Bearer " + encoded_jwt.decode("UTF-8"),) return client
def post(self, request, *args, **kwargs): """ ... """ serializer = self.serializer_class( data=request.data, context={"request": request}, ) serializer.is_valid(raise_exception=True) user = serializer.validated_data["user"] user_serializer = UserHeavySerializer(user) token, _ = Token.objects.get_or_create(user=user) key = settings.KEY_HS256 encoded_jwt = jwt.encode( { "token": token.key, "user": user_serializer.data }, key, algorithm="HS256") return Response(encoded_jwt.decode("UTF-8"), status=status.HTTP_200_OK)