def test_get_subscription_info(internal_api, stub): stub.register(RegisterRequest(user_id=user_id)) # Request subscription details message = "get subscription info" request_signature = Cryptographer.sign(message.encode("utf-8"), user_sk) response = stub.get_subscription_info( GetSubscriptionInfoRequest(signature=request_signature)) assert isinstance(response, GetUserResponse)
def test_get_subscription_info_non_registered(internal_api, stub): # Now let's try sending an invalid signature with the correct user key, but the wrong message signed. message = "wrong message" wrong_signature = Cryptographer.sign(message.encode("utf-8"), user_sk) with pytest.raises(grpc.RpcError) as e: stub.get_subscription_info( GetSubscriptionInfoRequest(signature=wrong_signature)) assert e.value.code() == grpc.StatusCode.UNAUTHENTICATED assert "User not found. Have you registered?" in e.value.details()
def test_get_subscription_info_bitcoind_crash(internal_api, stub, monkeypatch): monkeypatch.setattr(internal_api.watcher, "get_subscription_info", mock_connection_refused_return) message = "get subscription info" request_signature = Cryptographer.sign(message.encode("utf-8"), user_sk) with pytest.raises(grpc.RpcError) as e: stub.get_subscription_info( GetSubscriptionInfoRequest(signature=request_signature)) assert e.value.code() == grpc.StatusCode.UNAVAILABLE assert "Service unavailable" in e.value.details()
def get_subscription_info(self): """ Gives information about a user's subscription from the watchtower. Only a user who has registered can retrieve this information. User must provide the correct signature. Returns: :obj:`str`: A json formatted dictionary containing information about the user's subscription. Returns not found if the user is not yet registered with the watchtower. """ # Getting the real IP if the server is behind a reverse proxy remote_addr = get_remote_addr() # Check that data type and content are correct. Abort otherwise. try: request_data = get_request_data_json(request) except InvalidParameter as e: self.logger.info("Received invalid get_subscription_info request", from_addr="{}".format(remote_addr)) return jsonify({ "error": str(e), "error_code": errors.INVALID_REQUEST_FORMAT }), HTTP_BAD_REQUEST self.logger.info("Received get_subscription_info request", from_addr="{}".format(remote_addr)) try: r = self.stub.get_subscription_info( GetSubscriptionInfoRequest( signature=request_data.get("signature"))) response = intify( json_format.MessageToDict(r.user, including_default_value_fields=True, preserving_proto_field_name=True)) rcode = HTTP_OK except grpc.RpcError as e: rcode = HTTP_BAD_REQUEST response = { "error": e.details(), "error_code": errors.APPOINTMENT_INVALID_SIGNATURE_OR_SUBSCRIPTION_ERROR, } return jsonify(response), rcode
def test_get_subscription_info(internal_api, stub, monkeypatch): # Requesting the subscription info for a registered user should work # Mock the user being there. Data is not relevant since we only care about the type of response. subscription_info = UserInfo(100, [], 1000) monkeypatch.setattr(internal_api.watcher, "get_subscription_info", lambda x: (subscription_info, [])) # Request subscription details message = "get subscription info" request_signature = Cryptographer.sign(message.encode("utf-8"), user_sk) response = stub.get_subscription_info( GetSubscriptionInfoRequest(signature=request_signature)) assert isinstance(response, GetUserResponse)
def test_get_subscription_info_non_registered(internal_api, stub, monkeypatch): # Requesting the subscription info for a non-registered user should fail # Mock the user not being there. monkeypatch.setattr(internal_api.watcher, "get_subscription_info", raise_auth_failure) message = "get subscription info" signature = Cryptographer.sign(message.encode("utf-8"), user_sk) with pytest.raises(grpc.RpcError) as e: stub.get_subscription_info( GetSubscriptionInfoRequest(signature=signature)) assert e.value.code() == grpc.StatusCode.UNAUTHENTICATED assert "User not found. Have you registered?" in e.value.details()
def test_get_subscription_info_expired(internal_api, stub, monkeypatch): # Requesting the subscription info for expired users should fail # Mock the user not being there. monkeypatch.setattr(internal_api.watcher, "get_subscription_info", raise_subscription_expired) # Request subscription details message = "get subscription info" request_signature = Cryptographer.sign(message.encode("utf-8"), user_sk) with pytest.raises(grpc.RpcError) as e: stub.get_subscription_info( GetSubscriptionInfoRequest(signature=request_signature)) assert e.value.code() == grpc.StatusCode.UNAUTHENTICATED assert "Your subscription expired at" in e.value.details()
def test_get_subscription_info_expired(internal_api, stub): stub.register(RegisterRequest(user_id=user_id)) # Modify the user data so the subscription has already ended expiry = internal_api.watcher.block_processor.get_block_count( ) - internal_api.watcher.gatekeeper.expiry_delta - 1 internal_api.watcher.gatekeeper.registered_users[ user_id].subscription_expiry = expiry # Request subscription details message = "get subscription info" request_signature = Cryptographer.sign(message.encode("utf-8"), user_sk) with pytest.raises(grpc.RpcError) as e: stub.get_subscription_info( GetSubscriptionInfoRequest(signature=request_signature)) assert e.value.code() == grpc.StatusCode.UNAUTHENTICATED assert "Your subscription expired at" in e.value.details()