Skip to content

Commit

Permalink
Add test for getfolderpublink
Browse files Browse the repository at this point in the history
  • Loading branch information
tomgross committed Jun 23, 2023
1 parent d009b6b commit 643e442
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 26 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -30,7 +30,7 @@ report.html
!.gitignore
!.gitkeep
!.travis.yml
!src/pycloud
!src/pcloud
.cache
.eggs
.tox
Expand Down
6 changes: 3 additions & 3 deletions CHANGES.rst
Expand Up @@ -37,9 +37,9 @@ Changelog

- Python 3.10 compatibility and dependency updates
- Change port of test server 5000 -> 5023
- Add *getpubzip* API method (https://github.com/tomgross/pycloud/issues/51)
- Add *getpubzip* API method (https://github.com/tomgross/pcloud/issues/51)
- Allow uploading BIG files by using MultipartEncoder of requests_toolbelt
(https://github.com/tomgross/pycloud/issues/25, https://github.com/tomgross/pycloud/issues/44)
(https://github.com/tomgross/pcloud/issues/25, https://github.com/tomgross/pcloud/issues/44)
- Log login process
[tomgross]

Expand Down Expand Up @@ -114,7 +114,7 @@ Changelog
------------------

- Fix error while using makedirs from PyFilesystem
https://github.com/tomgross/pycloud/issues/10
https://github.com/tomgross/pcloud/issues/10
[blasterspike]

- Test and claim Python 3.7 compatibility
Expand Down
20 changes: 10 additions & 10 deletions CONTRIBUTING.md
Expand Up @@ -29,20 +29,20 @@ All types of contributions are encouraged and valued. See the [Table of Contents
## Code of Conduct

This project and everyone participating in it is governed by the
[pCloud Python API Code of Conduct](https://github.com/tomgross/pycloudblob/master/CODE_OF_CONDUCT.md).
[pCloud Python API Code of Conduct](https://github.com/tomgross/pcloud/blob/master/CODE_OF_CONDUCT.md).
By participating, you are expected to uphold this code. Please report unacceptable behavior
to <itconsense@gmail.com>.


## I Have a Question

> If you want to ask a question, we assume that you have read the available [Documentation](https://github.com/tomgross/pycloud/blob/master/README.rst).
> If you want to ask a question, we assume that you have read the available [Documentation](https://github.com/tomgross/pcloud/blob/master/README.rst).
Before you ask a question, it is best to search for existing [Issues](https://github.com/tomgross/pycloud/issues) that might help you. In case you have found a suitable issue and still need clarification, you can write your question in this issue. It is also advisable to search the internet for answers first.
Before you ask a question, it is best to search for existing [Issues](https://github.com/tomgross/pcloud/issues) that might help you. In case you have found a suitable issue and still need clarification, you can write your question in this issue. It is also advisable to search the internet for answers first.

If you then still feel the need to ask a question and need clarification, we recommend the following:

- Open an [Issue](https://github.com/tomgross/pycloud/issues/new).
- Open an [Issue](https://github.com/tomgross/pcloud/issues/new).
- Provide as much context as you can about what you're running into.
- Provide project and platform versions (nodejs, npm, etc), depending on what seems relevant.

Expand Down Expand Up @@ -76,8 +76,8 @@ Depending on how large the project is, you may want to outsource the questioning
A good bug report shouldn't leave others needing to chase you up for more information. Therefore, we ask you to investigate carefully, collect information and describe the issue in detail in your report. Please complete the following steps in advance to help us fix any potential bug as fast as possible.

- Make sure that you are using the latest version.
- Determine if your bug is really a bug and not an error on your side e.g. using incompatible environment components/versions (Make sure that you have read the [documentation](https://github.com/tomgross/pycloud/blob/master/README.rst). If you are looking for support, you might want to check [this section](#i-have-a-question)).
- To see if other users have experienced (and potentially already solved) the same issue you are having, check if there is not already a bug report existing for your bug or error in the [bug tracker](https://github.com/tomgross/pycloudissues?q=label%3Abug).
- Determine if your bug is really a bug and not an error on your side e.g. using incompatible environment components/versions (Make sure that you have read the [documentation](https://github.com/tomgross/pcloud/blob/master/README.rst). If you are looking for support, you might want to check [this section](#i-have-a-question)).
- To see if other users have experienced (and potentially already solved) the same issue you are having, check if there is not already a bug report existing for your bug or error in the [bug tracker](https://github.com/tomgross/pcloud/issues?q=label%3Abug).
- Also make sure to search the internet (including Stack Overflow) to see if users outside of the GitHub community have discussed the issue.
- Collect information about the bug:
- Stack trace (Traceback)
Expand All @@ -94,7 +94,7 @@ A good bug report shouldn't leave others needing to chase you up for more inform
We use GitHub issues to track bugs and errors. If you run into an issue with the project:

- Open an [Issue](https://github.com/tomgross/pycloud/issues/new). (Since we can't be sure at this point whether it is a bug or not, we ask you not to talk about a bug yet and not to label the issue.)
- Open an [Issue](https://github.com/tomgross/pcloud/issues/new). (Since we can't be sure at this point whether it is a bug or not, we ask you not to talk about a bug yet and not to label the issue.)
- Explain the behavior you would expect and the actual behavior.
- Please provide as much context as possible and describe the *reproduction steps* that someone else can follow to recreate the issue on their own. This usually includes your code. For good bug reports you should isolate the problem and create a reduced test case.
- Provide the information you collected in the previous section.
Expand All @@ -116,14 +116,14 @@ This section guides you through submitting an enhancement suggestion for pCloud
#### Before Submitting an Enhancement

- Make sure that you are using the latest version.
- Read the [documentation](https://github.com/tomgross/pycloud/blob/master/README.rst) carefully and find out if the functionality is already covered, maybe by an individual configuration.
- Perform a [search](https://github.com/tomgross/pycloud/issues) to see if the enhancement has already been suggested. If it has, add a comment to the existing issue instead of opening a new one.
- Read the [documentation](https://github.com/tomgross/pcloud/blob/master/README.rst) carefully and find out if the functionality is already covered, maybe by an individual configuration.
- Perform a [search](https://github.com/tomgross/pcloud/issues) to see if the enhancement has already been suggested. If it has, add a comment to the existing issue instead of opening a new one.
- Find out whether your idea fits with the scope and aims of the project. It's up to you to make a strong case to convince the project's developers of the merits of this feature. Keep in mind that we want features that will be useful to the majority of our users and not just a small subset. If you're just targeting a minority of users, consider writing an add-on/plugin library.

<!-- omit in toc -->
#### How Do I Submit a Good Enhancement Suggestion?

Enhancement suggestions are tracked as [GitHub issues](https://github.com/tomgross/pycloud/issues).
Enhancement suggestions are tracked as [GitHub issues](https://github.com/tomgross/pcloud/issues).

- Use a **clear and descriptive title** for the issue to identify the suggestion.
- Provide a **step-by-step description of the suggested enhancement** in as many details as possible.
Expand Down
8 changes: 4 additions & 4 deletions README.rst
Expand Up @@ -2,8 +2,8 @@
pcloud - A Python API client for pCloud
==============================================================================

.. image:: https://github.com/tomgross/pycloud/actions/workflows/pycloud-test.yml/badge.svg
:target: https://github.com/tomgross/pycloud/actions
.. image:: https://github.com/tomgross/pcloud/actions/workflows/pcloud-test.yml/badge.svg
:target: https://github.com/tomgross/pcloud/actions

This Python **(Version >= 3.6 only!)** library provides a Python API to the pCloud storage.

Expand Down Expand Up @@ -139,8 +139,8 @@ The file contains the expected JSON result.
Contribute
==========

- Issue Tracker: https://github.com/tomgross/pycloud/issues
- Source Code: https://github.com/tomgross/pycloud
- Issue Tracker: https://github.com/tomgross/pcloud/issues
- Source Code: https://github.com/tomgross/pcloud

License
=======
Expand Down
25 changes: 23 additions & 2 deletions src/pcloud/api.py
Expand Up @@ -8,14 +8,15 @@
from urllib.parse import urlunsplit

import argparse
import datetime
import logging
import os.path
import requests
import sys
import zipfile


log = logging.getLogger("pycloud")
log = logging.getLogger("pcloud")
log.setLevel(logging.INFO)

handler = logging.StreamHandler(sys.stderr)
Expand Down Expand Up @@ -43,6 +44,19 @@ class OnlyPcloudError(NotImplementedError):
"""Feature restricted to pCloud"""


# Helpers


def to_api_datetime(dt):
"""Converter to a datetime structure the pCloud API understands
See https://docs.pcloud.com/structures/datetime.html
"""
if isinstance(dt, datetime.datetime):
return dt.isoformat()
return dt


def main():
parser = argparse.ArgumentParser(description="pCloud command line client")
parser.add_argument(
Expand Down Expand Up @@ -445,9 +459,16 @@ def sharefolder(self, **kwargs):
return self._do_request("sharefolder", **kwargs)

def listshares(self, **kwargs):
return self._do_request("listshares")
return self._do_request("listshares", **kwargs)

# Public links
@RequiredParameterCheck(("path", "folderid"))
def getfolderpublink(self, **kwargs):
expire = kwargs.get("expire")
if expire is not None:
kwargs["expire"] = to_api_datetime(expire)
return self._do_request("getfolderpublink", **kwargs)

@RequiredParameterCheck(("code",))
def getpubzip(self, unzip=False, **kwargs):
zipresponse = self._do_request(
Expand Down
2 changes: 1 addition & 1 deletion src/pcloud/oauth2.py
Expand Up @@ -52,7 +52,7 @@ def get_access_token(self):
http_server = HTTPServer(("localhost", PORT), HTTPServerHandler)

# Solution taken from https://stackoverflow.com/a/12651298
# There might be better ways than accessing the internal
# There might be better ways than accessing the internal
# _thread library for starting the http-server non-blocking
# but I did not found any ;-)
def start_server():
Expand Down
37 changes: 36 additions & 1 deletion src/pcloud/tests/test_api.py
Expand Up @@ -2,20 +2,55 @@
from pcloud import api
from pcloud.pcloudfs import PCloudFS

import datetime
import json
import os.path
import pytest


class NoOpSession(object):
kwargs = {}

def get(self, url, **kwargs):
self.kwargs = kwargs
self.kwargs["url"] = url
return self

def json(self):
return self.kwargs


class DummyPyCloud(api.PyCloud):
def __init__(self, username, password):
noop = False

def get_auth_token(self):
if self.noop:
self.auth_token = None
self.access_token = None
else:
return super(DummyPyCloud, self).get_auth_token()

def __init__(self, username, password, noop=False):
if noop:
self.noop = True
super(DummyPyCloud, self).__init__(username, password, endpoint="test")
if noop:
self.session = NoOpSession()


class DummyPCloudFS(PCloudFS):
factory = DummyPyCloud


def test_getfolderpublink():
api = DummyPyCloud("john", "doe", noop=True)
dt = datetime.datetime(2023, 10, 5, 12, 3, 12)
assert api.getfolderpublink(folderid=20, expire=dt) == {
"params": {"expire": "2023-10-05T12:03:12", "folderid": 20},
"url": "http://localhost:5023/getfolderpublink",
}


@pytest.mark.usefixtures("start_mock_server")
class TestPcloudApi(object):
def test_getdigest(self):
Expand Down
11 changes: 11 additions & 0 deletions src/pcloud/tests/test_helpers.py
@@ -0,0 +1,11 @@
from pcloud.api import to_api_datetime
import datetime


def test_to_api_datetime_dt():
dt = datetime.datetime(2023, 10, 5, 12, 3, 12)
assert to_api_datetime(dt) == "2023-10-05T12:03:12"


def test_to_api_datetime_iso():
assert to_api_datetime("2013-12-05T12:03:12") == "2013-12-05T12:03:12"
5 changes: 3 additions & 2 deletions src/pcloud/tests/test_integration.py
Expand Up @@ -74,7 +74,8 @@ def test_copyfile(pycloud, testfolder):
== "df745d42f69266c49141ea7270c45240cf883b9cdb6a14fffcdff33c04c5304c"
)


def test_listtokens(pycloud):
result = pycloud.listtokens()
assert result['result'] == 0
assert len(result['tokens']) > 1
assert result["result"] == 0
assert len(result["tokens"]) > 1
5 changes: 3 additions & 2 deletions src/pcloud/tests/test_oauth2.py
Expand Up @@ -62,7 +62,8 @@ def test_upload_download_roundrobin(pycloud_oauth2, testfolder):
result = pycloud_oauth2.file_close(fd=fd)
assert result["result"] == 0


def test_listtokens(pycloud_oauth2):
# listtokens endpoint is not available for OAuth authentication
# see https://github.com/tomgross/pycloud/issues/61
assert pycloud_oauth2.listtokens() == {'error': 'Log in required.', 'result': 1000}
# see https://github.com/tomgross/pcloud/issues/61
assert pycloud_oauth2.listtokens() == {"error": "Log in required.", "result": 1000}

0 comments on commit 643e442

Please sign in to comment.