Skip to content
This repository has been archived by the owner on Mar 28, 2022. It is now read-only.

Commit

Permalink
test: Add test for invalid password.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewjw committed Aug 17, 2020
1 parent f7a89c9 commit abf3a89
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion code_style.sh
Expand Up @@ -2,4 +2,4 @@

set -e

pycodestyle bin/ zyxelprometheus/
pycodestyle bin/ zyxelprometheus/ tests/
21 changes: 18 additions & 3 deletions tests/test_login.py
Expand Up @@ -20,15 +20,30 @@

import responses

from zyxelprometheus import login
from zyxelprometheus import login, InvalidPassword


class TestLogin(unittest.TestCase):
@responses.activate
def test_correct_password(self):
responses.add(responses.POST, 'https://192.168.1.1/UserLogin', status=200)
responses.add(responses.POST, 'https://192.168.1.1/UserLogin',
status=200)

login("https://192.168.1.1", "admin", "testpassword")


self.assertEqual(1, len(responses.calls))
data = json.loads(responses.calls[0].request.body)
self.assertEqual("admin", data["Input_Account"])
self.assertEqual(b"testpassword", b64decode(data["Input_Passwd"]))

@responses.activate
def test_wrong_password(self):
responses.add(responses.POST, 'https://192.168.1.1/UserLogin',
status=401)

with self.assertRaises(InvalidPassword):
login("https://192.168.1.1", "admin", "testpassword")

self.assertEqual(1, len(responses.calls))
data = json.loads(responses.calls[0].request.body)
self.assertEqual("admin", data["Input_Account"])
Expand Down
1 change: 1 addition & 0 deletions zyxelprometheus/__init__.py
Expand Up @@ -14,6 +14,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from .exceptions import InvalidPassword
from .login import login
from .prometheus import prometheus
from .scrape import scrape_xdsl, scrape_traffic
Expand Down
18 changes: 18 additions & 0 deletions zyxelprometheus/exceptions.py
@@ -0,0 +1,18 @@
# zyxelprometheus
# Copyright (C) 2020 Andrew Wilkinson
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

class InvalidPassword(Exception):
pass
4 changes: 4 additions & 0 deletions zyxelprometheus/login.py
Expand Up @@ -19,6 +19,8 @@

import requests

from .exceptions import InvalidPassword

# https://192.168.1.1/UserLogin
# {"Input_Account":"admin","Input_Passwd":"c2hhZ2dpZTE:",
# "currLang":"en","RememberPassword":0,"SHA512_password":false}
Expand All @@ -34,6 +36,8 @@ def login(host, username, password):
"RememberPassword": 0,
"SHA512_password": False
}))
if r.status_code == 401:
raise InvalidPassword("Invalid username or password.")
r.raise_for_status()

return session

0 comments on commit abf3a89

Please sign in to comment.