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"])
예제 #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"])
예제 #3
0
def route_number_add_user():
    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_exists(conn, number_id)
        user_id = user.form_user_id(request)
        user.verify_exists(conn, user_id)
        number.verify_user_not_added(conn, number_id, user_id)
        number.add_user(conn, number_id, user_id)
        return ""
 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))
예제 #5
0
    def test_route_remove_number_user__with_invalid_auth_token(self):
        user_id = "*****@*****.**"
        number_id = "com.myapp"
        invalid_auth_token = "ooo000"

        # Given
        with postgres.get_db_conn() as conn:
            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(invalid_auth_token))
        # Then
        self.assertEqual(401, response.status_code)
        response_json = json.loads(response.data)
        self.assertEqual("Authentication failed", response_json["error"])
예제 #6
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"))
예제 #7
0
    def test_route_remove_number_user__with_invalid_user(self):
        admin_auth_token = "abc123"
        number_id = "com.myapp"
        user_id = "*****@*****.**"

        # Given
        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)
            number.add_user(conn, number_id, user_id)
        # When
        invalid_user_id = "*****@*****.**"
        response = self.app.post("/number/user/remove",
                                 data={"user_id": invalid_user_id, "number_id": 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"User {invalid_user_id} not found", response_json["error"])
예제 #8
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"))