-
Notifications
You must be signed in to change notification settings - Fork 208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes #308 Include tenant-id when using files api (#1) #346
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import os.path | ||
import unittest | ||
from time import time | ||
from unittest.mock import patch | ||
|
||
from xero import Xero | ||
from xero.auth import OAuth2Credentials | ||
|
||
|
||
class FilesManagerTest(unittest.TestCase): | ||
def setUp(self): | ||
super().setUp() | ||
# Create an expired token to be used by tests | ||
self.expired_token = { | ||
"access_token": "1234567890", | ||
"expires_in": 1800, | ||
"token_type": "Bearer", | ||
"refresh_token": "0987654321", | ||
"expires_at": time(), | ||
} | ||
|
||
self.filepath = "test_file.txt" | ||
with open(self.filepath, "w") as f: | ||
f.write("test") | ||
|
||
def tearDown(self): | ||
os.remove(self.filepath) | ||
|
||
@patch("requests.get") | ||
def test_tenant_is_used_in_xero_request(self, r_get): | ||
credentials = OAuth2Credentials( | ||
"client_id", "client_secret", token=self.expired_token, tenant_id="12345" | ||
) | ||
xero = Xero(credentials) | ||
# Just return any old response | ||
r_get.return_value = None | ||
try: | ||
xero.filesAPI.files.all() | ||
except: # NOQA: E722 | ||
pass | ||
|
||
self.assertEqual(r_get.call_args[1]["headers"]["Xero-tenant-id"], "12345") | ||
|
||
@patch("requests.post") | ||
def test_upload_file_as_path(self, r_get): | ||
credentials = OAuth2Credentials( | ||
"client_id", "client_secret", token=self.expired_token, tenant_id="12345" | ||
) | ||
xero = Xero(credentials) | ||
r_get.return_value = None | ||
try: | ||
xero.filesAPI.files.upload_file(path=self.filepath) | ||
except: # NOQA: E722 | ||
pass | ||
|
||
self.assertIn(self.filepath, r_get.call_args[1]["files"]) | ||
|
||
@patch("requests.post") | ||
def test_upload_file_as_file(self, r_get): | ||
credentials = OAuth2Credentials( | ||
"client_id", "client_secret", token=self.expired_token, tenant_id="12345" | ||
) | ||
xero = Xero(credentials) | ||
r_get.return_value = None | ||
try: | ||
with open(self.filepath) as f: | ||
xero.filesAPI.files.upload_file( | ||
file=f, filename=os.path.basename(self.filepath) | ||
) | ||
except: # NOQA: E722 | ||
pass | ||
|
||
self.assertIn(self.filepath, r_get.call_args[1]["files"]) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit of a red flag - bare exceptions are flagged by flake8 for a reason; and in this case, I can't see a reason we should be raising an exception at all. As best as I can make out, we just need a more realistic mock return value.