Skip to content

Commit

Permalink
Merge pull request #308 from markkuleinio/master
Browse files Browse the repository at this point in the history
Fixes #304: Change Record._endpoint_from_url() to count URL fields from the end
  • Loading branch information
Zach Moody authored Dec 15, 2020
2 parents 4c3b0f8 + 12b38cb commit 5173efd
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
6 changes: 5 additions & 1 deletion pynetbox/core/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,11 @@ def list_parser(list_item):
setattr(self, k, v)

def _endpoint_from_url(self, url):
app, name = urlsplit(url).path.split("/")[2:4]
# Remove the base URL from the beginning
url = url[len(self.api.base_url):]
if url.startswith("/"):
url = url[1:]
app, name = urlsplit(url).path.split("/")[:2]
return getattr(pynetbox.core.app.App(self.api, app), name)

def full_details(self):
Expand Down
45 changes: 44 additions & 1 deletion tests/unit/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,57 @@ def test_nested_write(self):
"http://localhost:8080/api/test-app/test-endpoint/321/",
)

def test_nested_write_with_directory_in_base_url(self):
app = Mock()
app.token = "abc123"
app.base_url = "http://localhost:8080/testing/api"
endpoint = Mock()
endpoint.name = "test-endpoint"
test = Record(
{
"id": 123,
"name": "test",
"child": {
"id": 321,
"name": "test123",
"url": "http://localhost:8080/testing/api/test-app/test-endpoint/321/",
},
},
app,
endpoint,
)
test.child.name = "test321"
test.child.save()
self.assertEqual(
app.http_session.patch.call_args[0][0],
"http://localhost:8080/testing/api/test-app/test-endpoint/321/",
)

def test_endpoint_from_url(self):
api = Mock()
api.base_url = "http://localhost:8080/api"
test = Record(
{
"id": 123,
"name": "test",
"url": "http://localhost:8080/api/test-app/test-endpoint/1/",
},
Mock(),
api,
None,
)
ret = test._endpoint_from_url(test.url)
self.assertEqual(ret.name, "test-endpoint")

def test_endpoint_from_url_with_directory_in_base_url(self):
api = Mock()
api.base_url = "http://localhost:8080/testing/api"
test = Record(
{
"id": 123,
"name": "test",
"url": "http://localhost:8080/testing/api/test-app/test-endpoint/1/",
},
api,
None,
)
ret = test._endpoint_from_url(test.url)
Expand Down

0 comments on commit 5173efd

Please sign in to comment.