def test_route_number_add_user__already_added(self):
     # Given
     admin_auth_token = "abc123"
     number_id = "com.myapp"
     user_id = "*****@*****.**"
     password = "******"
     with postgres.get_db_conn() as conn:
         user.add(conn,
                  "admin",
                  "oranges",
                  auth_token=admin_auth_token,
                  is_admin=True)
         user.add(conn, user_id, password)
         number.add(conn, number_id)
         number.add_user(conn, number_id, user_id)
     # When
     response = self.app.post(
         "/number/user/add",
         data={
             "user_id": user_id,
             "number_id": number_id
         },
         headers=self.authorization_custom(admin_auth_token))
     # Then
     self.assertEqual(400, response.status_code)
     response_json = json.loads(response.data)
     self.assertEqual(f"User {user_id} already added to number {number_id}",
                      response_json["error"])
Beispiel #2
0
 def test_route_number_set__with_invalid_auth_token(self):
     # Given
     user_id = "*****@*****.**"
     admin_auth_token = "abc123"
     number_id = "com.myapp"
     with postgres.get_db_conn() as conn:
         user.add(conn,
                  user_id,
                  "oranges",
                  auth_token=admin_auth_token,
                  is_admin=True)
         number.add(conn, number_id)
         number.add_user(conn, number_id, user_id)
     # When
     invalid_auth_token = "ooo000"
     response = self.app.post(
         "/number/set",
         data={
             "number_id": number_id,
             "new_number": 2
         },
         headers=self.authorization_custom(invalid_auth_token))
     # Then
     self.assertEqual(401, response.status_code)
     response_json = json.loads(response.data)
     self.assertEqual("Authentication failed", response_json["error"])
 def test_route_number_add_user__with_invalid_number(self):
     # Given
     admin_auth_token = "abc123"
     number_id = "com.myapp"
     with postgres.get_db_conn() as conn:
         user.add(conn,
                  "admin",
                  "oranges",
                  auth_token=admin_auth_token,
                  is_admin=True)
         number.add(conn, number_id)
     # When
     user_id = "*****@*****.**"
     invalid_number_id = "com.fakeapp"
     response = self.app.post(
         "/number/user/add",
         data={
             "user_id": user_id,
             "number_id": invalid_number_id
         },
         headers=self.authorization_custom(admin_auth_token))
     # Then
     self.assertEqual(404, response.status_code)
     response_json = json.loads(response.data)
     self.assertEqual(f"Number {invalid_number_id} not found",
                      response_json["error"])
Beispiel #4
0
def route_number_add():
    with postgres.get_db_conn() as conn:
        auth.check_auth_token(conn,
                              auth.header_auth_token(request),
                              check_is_admin=True)
        number_id = number.form_id(request)
        number.verify_not_exists(conn, number_id)
        number.add(conn, number_id)
        return ""
Beispiel #5
0
 def test_route_add_number__existing(self):
     # Given
     admin_auth_token = "abc123"
     number_id = "com.myapp"
     with postgres.get_db_conn() as conn:
         user.add(conn, "admin", "oranges", auth_token=admin_auth_token, is_admin=True)
         number.add(conn, number_id)
     # When
     response = self.app.post("/number/add",
                              data={"number_id": number_id},
                              headers=self.authorization_custom(admin_auth_token))
     # Then
     self.assertEqual(400, response.status_code)
     response_json = json.loads(response.data)
     self.assertEqual(f"Number {number_id} already exists", response_json["error"])
Beispiel #6
0
 def test_route_remove_number__with_invalid_auth_token(self):
     # Given
     invalid_auth_token = "ooo000"
     number_id = "com.myapp"
     with postgres.get_db_conn() as conn:
         number.add(conn, number_id)
     # When
     response = self.app.post(
         "/number/remove",
         data={"number_id": number_id},
         headers=self.authorization_custom(invalid_auth_token))
     # Then
     self.assertEqual(401, response.status_code)
     response_json = json.loads(response.data)
     self.assertEqual("Authentication failed", response_json["error"])
 def test_route_number_get_current__with_valid_auth_token(self):
     # Given
     user_id = "*****@*****.**"
     user_auth_token = "abc123"
     number_id = "com.myapp"
     with postgres.get_db_conn() as conn:
         user.add(conn, user_id, "oranges", auth_token=user_auth_token, is_admin=False)
         number.add(conn, number_id)
         number.add_user(conn, number_id, user_id)
     # When
     response = self.app.post("/number/current",
                              data={"number_id": number_id},
                              headers=self.authorization_custom(user_auth_token))
     # Then
     self.assertEqual(200, response.status_code)
     self.assertEqual(0, int(response.data))
Beispiel #8
0
    def test_route_remove_number_user__with_admin_auth_token(self):
        admin_auth_token = "abc123"
        user_id = "*****@*****.**"
        number_id = "com.myapp"

        # Given
        with postgres.get_db_conn() as conn:
            user.add(conn, "admin", "oranges", auth_token=admin_auth_token, is_admin=True)
            user.add(conn, user_id, "sesame")
            number.add(conn, number_id)
            number.add_user(conn, number_id, user_id)
        # When
        response = self.app.post("/number/user/remove",
                                 data={"user_id": user_id, "number_id": number_id},
                                 headers=self.authorization_custom(admin_auth_token))
        # Then
        self.assertEqual(200, response.status_code)
        self.assertEqual("", response.data.decode("utf-8"))
Beispiel #9
0
 def test_route_remove_number__with_user_auth_token(self):
     # Given
     user_auth_token = "xyz789"
     number_id = "com.myapp"
     with postgres.get_db_conn() as conn:
         user.add(conn,
                  "admin",
                  "oranges",
                  auth_token=user_auth_token,
                  is_admin=False)
         number.add(conn, number_id)
     # When
     response = self.app.post(
         "/number/remove",
         data={"number_id": number_id},
         headers=self.authorization_custom(user_auth_token))
     # Then
     self.assertEqual(401, response.status_code)
     response_json = json.loads(response.data)
     self.assertEqual("Authentication failed", response_json["error"])
Beispiel #10
0
 def test_route_number_set__with_valid_auth_token(self):
     # Given
     user_id = "*****@*****.**"
     admin_auth_token = "abc123"
     number_id = "com.myapp"
     with postgres.get_db_conn() as conn:
         user.add(conn,
                  user_id,
                  "oranges",
                  auth_token=admin_auth_token,
                  is_admin=True)
         number.add(conn, number_id)
         number.add_user(conn, number_id, user_id)
     # When
     response = self.app.post(
         "/number/set",
         data={
             "number_id": number_id,
             "new_number": 2
         },
         headers=self.authorization_custom(admin_auth_token))
     # Then
     self.assertEqual(200, response.status_code)
     self.assertEqual("", response.data.decode("utf-8"))