Ejemplo n.º 1
0
    def test_no_filter(self) -> None:
        headers = {
            "Authorization": "Bearer {}".format(add_token("admin", "desc1")),
        }
        add_server(ip="1", port=1, public_key="ssh-rsa ...", token_id=1)
        add_server(ip="2", port=2, public_key="ssh-ed25519 ...", token_id=1)

        res = self.client.get("/api/v1/server", headers=headers)
        data = json.loads(res.data.decode("utf-8"))
        servers = data["servers"]

        self.assertEqual(len(servers), 2)

        self.assertEqual(servers[0]["id"], 1)
        self.assertEqual(servers[0]["ip"], "1")
        self.assertEqual(servers[0]["port"], 1)
        self.assertEqual(servers[0]["key_type"], "ssh-rsa")
        self.assertEqual(servers[0]["key_data"], "...")
        self.assertEqual(servers[0]["key_comment"], "")

        self.assertEqual(servers[1]["id"], 2)
        self.assertEqual(servers[1]["ip"], "2")
        self.assertEqual(servers[1]["port"], 2)
        self.assertEqual(servers[1]["key_type"], "ssh-ed25519")
        self.assertEqual(servers[1]["key_data"], "...")
        self.assertEqual(servers[1]["key_comment"], "")
Ejemplo n.º 2
0
    def test_ip(self) -> None:
        add_token("server", "desc")
        add_server("ip1", 1111, "ssh-rsa data comment", 1)
        add_server("ip2", 2222, "ssh-ed25519 data comment", 1)

        servers = get_servers(ip="ip1")
        self.assertEqual(len(servers), 1)
        self.assertEqual(servers[0].ip, "ip1")
Ejemplo n.º 3
0
    def test_strip_key(self) -> None:
        add_token("server", "desc")
        add_server("ip", 1234, "   ssh-rsa    data     comment    \n\n\n", 1)

        servers = get_servers()
        self.assertEqual(servers[0].key_type, "ssh-rsa")
        self.assertEqual(servers[0].key_data, "data")
        self.assertEqual(servers[0].key_comment, "comment")
Ejemplo n.º 4
0
    def test_all(self) -> None:
        add_token("server", "desc")

        for i in range(10):
            add_server("ip", i, "ssh-rsa data comment", 1)

        servers = get_servers()
        self.assertEqual(len(servers), 10)
Ejemplo n.º 5
0
    def test_invalid_id(self) -> None:
        with self.assertRaises(ServerError):
            delete_server(0)

        add_token("admin", "desc")
        add_server("10.0.0.1", 1234, "ssh-rsa data", 1)
        add_server("10.0.0.2", 4321, "ssh-rsa data", 1)

        with self.assertRaises(ServerError):
            delete_server(0)

        self.assertEqual(len(get_servers()), 2)
Ejemplo n.º 6
0
    def test_success(self) -> None:
        add_token("server", "desc")
        add_server("ip", 1234, "ssh-rsa data comment", 1)

        servers = get_servers()
        self.assertEqual(len(servers), 1)
        self.assertEqual(servers[0].ip, "ip")
        self.assertEqual(servers[0].port, 1234)
        self.assertEqual(servers[0].key_type, "ssh-rsa")
        self.assertEqual(servers[0].key_data, "data")
        self.assertEqual(servers[0].key_comment, "comment")
        self.assertEqual(servers[0].token_id, 1)
Ejemplo n.º 7
0
    def test_invalid_id(self) -> None:
        headers = {
            "Authorization": "Bearer {}".format(add_token("admin", "desc")),
        }

        data = {
            "id": 2,
        }

        add_server("10.0.0.1", 11, "ssh-rsa data", 1)

        res = self.client.delete("/api/v1/server", headers=headers, data=data)
        data = json.loads(res.data.decode("utf-8"))
        self.assertEqual(data["message"], "invalid server id 2")
        self.assertEqual(res.status_code, 400)
Ejemplo n.º 8
0
    def test_unauthorized_server(self) -> None:
        headers = {
            "Authorization": "Bearer {}".format(add_token("server", "desc")),
        }

        data = {
            "id": 1,
        }

        add_server("10.0.0.1", 11, "ssh-rsa data", 1)

        res = self.client.delete("/api/v1/server", headers=headers, data=data)
        data = json.loads(res.data.decode("utf-8"))
        self.assertEqual(data["message"], "Permission denied")
        self.assertEqual(res.status_code, 401)
Ejemplo n.º 9
0
    def test_success(self) -> None:
        add_token("admin", "desc0")

        add_server("10.0.0.1", 11, "ssh-rsa data", 1)
        add_server("10.0.0.2", 22, "ecdsa-sha2-nistp521 data", 1)
        add_server("10.0.0.3", 33, "ssh-ed25519 data", 1)
        self.assertEqual(len(get_servers()), 3)

        delete_server(2)

        servers = get_servers()
        self.assertEqual(len(servers), 2)
        self.assertEqual(servers[0].ip, "10.0.0.1")
        self.assertEqual(servers[1].ip, "10.0.0.3")
Ejemplo n.º 10
0
    def test_success(self) -> None:
        add_token("server", "desc")

        headers = {
            "Authorization": "Bearer {}".format(add_token("admin", "desc")),
        }

        data = {
            "id": 1,
        }

        add_server("10.0.0.1", 11, "ssh-rsa data", 1)
        add_server("10.0.0.2", 22, "ssh-rsa data", 1)
        add_server("10.0.0.2", 33, "ssh-rsa data", 1)

        res = self.client.delete("/api/v1/server", headers=headers, data=data)
        data = json.loads(res.data.decode("utf-8"))
        self.assertEqual(data["message"], "deleted")
        self.assertEqual(res.status_code, 200)

        res = self.client.get("/api/v1/server", headers=headers)
        servers = json.loads(res.data.decode("utf-8"))["servers"]
        self.assertEqual(len(servers), 2)
Ejemplo n.º 11
0
 def test_invalid_key_data(self) -> None:
     with self.assertRaises(ServerError):
         add_server("ip", 1234, "ssh-ed25519 abc comment", 1)
Ejemplo n.º 12
0
 def test_invalid_key_type_sh_rsa(self) -> None:
     with self.assertRaises(ServerError):
         add_server("ip", 1234, "sh-rsa data comment", 1)
Ejemplo n.º 13
0
 def test_invalid_key(self) -> None:
     with self.assertRaises(ServerError):
         add_server("ip", 1234, "", 1)