Beispiel #1
0
def test_server_authenticate(database, tracer):
    password = "******"
    ph = PasswordHasher()
    password_hash = ph.hash(password)
    user = UserFactory.create(password=password_hash)
    assert database.query(User).one()

    user_store = UserDataStore(database, tracer)
    user_case = UserUseCase(user_store, tracer)

    order_store = OrderDataStore(database, tracer)
    order_case = OrderUseCase(order_store, tracer)

    date = datetime.date.today()
    holiday_store = HolidayDataStore(date, tracer)
    holiday_case = HolidayUseCase(holiday_store, tracer)

    balance_client = BalanceClient(settings.BALANCE_TOKEN)
    balance_case = BalanceUseCase(order_case, balance_client, tracer)

    authentication_case = AuthenticationUseCase(user_case, tracer)

    case = PromotionUseCase(discounts=[holiday_case, user_case], tracer=tracer)

    servicer = PromotionServicer(case, user_case, order_case, balance_case,
                                 authentication_case, tracer)

    request = AuthenticateRequest(email=user.email, password=password)
    result = servicer.Authenticate(request, None)

    assert result.id_token.startswith('ey')
Beispiel #2
0
def test_server_retrieve_promotion(database, tracer):
    user = UserFactory.create()
    assert database.query(User).one()

    user_store = UserDataStore(database, tracer)
    user_case = UserUseCase(user_store, tracer)

    order_store = OrderDataStore(database, tracer)
    order_case = OrderUseCase(order_store, tracer)

    date = datetime.date.today()
    holiday_store = HolidayDataStore(date, tracer)
    holiday_case = HolidayUseCase(holiday_store, tracer)

    balance_client = BalanceClient(settings.BALANCE_TOKEN)
    balance_case = BalanceUseCase(order_case, balance_client, tracer)

    authentication_case = AuthenticationUseCase(user_case, tracer)

    case = PromotionUseCase(discounts=[holiday_case, user_case], tracer=tracer)

    servicer = PromotionServicer(case, user_case, order_case, balance_case,
                                 authentication_case, tracer)

    request = RetrievePromotionRequest(user_id=str(user.id),
                                       product_id=str(uuid.uuid4()).encode())
    result = servicer.RetrievePromotion(request, None)

    assert result.discounts[0].pct == 10
Beispiel #3
0
def test_cashback_from_thousand_to_thousand_five_hundred(database, tracer):
    OrderFactory.create_batch(10, amount_cents=130000)
    assert database.query(Order).count() == 10

    user_store = OrderDataStore(database, tracer)
    user_case = OrderUseCase(user_store, tracer)

    order_store = OrderDataStore(database, tracer)
    order_case = OrderUseCase(order_store, tracer)

    balance_client = BalanceClient(settings.BALANCE_TOKEN)
    balance_case = BalanceUseCase(order_case, balance_client, tracer)

    authentication_case = AuthenticationUseCase(user_case, tracer)

    case = PromotionUseCase(discounts=[], tracer=tracer)

    servicer = PromotionServicer(case, user_case, order_case, balance_case,
                                 authentication_case, tracer)

    request = CreateUserRequest()
    response = servicer.ListOrdersWithCashback(request, None)

    assert len(response.orders) == 10
    result = response.orders[0]
    assert result.amount_cashback_cents == 19500
    assert result.cashback_percentage == 15.0
Beispiel #4
0
def test_server_create_order(database, tracer):
    user_store = OrderDataStore(database, tracer)
    user_case = OrderUseCase(user_store, tracer)

    order_store = OrderDataStore(database, tracer)
    order_case = OrderUseCase(order_store, tracer)

    balance_client = BalanceClient(settings.BALANCE_TOKEN)
    balance_case = BalanceUseCase(order_case, balance_client, tracer)

    authentication_case = AuthenticationUseCase(user_case, tracer)

    case = PromotionUseCase(discounts=[], tracer=tracer)

    servicer = PromotionServicer(case, user_case, order_case, balance_case,
                                 authentication_case, tracer)

    date = datetime.date.today()
    date = Date(year=date.year, month=date.month, day=date.day)
    order_code = uuid.uuid4()
    request = CreateOrderRequestResponse(
        code=str(order_code).encode(),
        identity="03303441965",
        amount_cents=100,
        status="validating",
        date=date,
    )
    result = servicer.CreateOrder(request, None)

    assert database.query(Order).one()
    assert result.status == "validating"
Beispiel #5
0
def _grpc():
    """Start discount gRPC server."""

    logging.basicConfig(level=logging.INFO)

    engine = create_engine(settings.DATABASE_URL)
    Session = sessionmaker(bind=engine)
    connection = engine.connect()
    session = Session(bind=connection)

    exporter = ConsoleSpanExporter()
    if settings.TRACER_ENDPOINT_HOST and settings.TRACER_ENDPOINT_PORT:
        exporter = JaegerSpanExporter(
            service_name="promotion-grpc",
            agent_host_name=settings.TRACER_ENDPOINT_HOST,
            agent_port=settings.TRACER_ENDPOINT_PORT,
        )

    trace.set_preferred_tracer_source_implementation(lambda T: TracerSource())

    tracer = trace.get_tracer(__name__)

    ## span_processor = BatchExportSpanProcessor(exporter)
    span_processor = SimpleExportSpanProcessor(exporter)

    trace.tracer_source().add_span_processor(span_processor)

    tracer = trace.get_tracer(__name__)

    user_store = UserDataStore(session, tracer)
    user_case = UserUseCase(user_store, tracer)

    order_store = OrderDataStore(session, tracer)
    order_case = OrderUseCase(order_store, tracer)

    balance_client = BalanceClient(settings.BALANCE_TOKEN)
    balance_case = BalanceUseCase(order_case, balance_client, tracer)

    holiday_store = HolidayDataStore(settings.BLACK_FRIDAY_DATE, tracer)
    holiday_case = HolidayUseCase(holiday_store, tracer)

    authentication_case = AuthenticationUseCase(user_case, tracer)

    case = PromotionUseCase(discounts=[holiday_case, user_case], tracer=tracer)

    servicer = PromotionServicer(case, user_case, order_case, balance_case,
                                 authentication_case, tracer)
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    service.add_PromotionAPIServicer_to_server(servicer, server)
    logger.info("Listenning on port 50051.")
    server.add_insecure_port("[::]:50051")
    server.start()
    server.wait_for_termination()
def test_discount_only_by_holiday(user_store_mock, tracer):
    user_store_mock.user.return_value = None
    user_case = UserUseCase(user_store_mock, tracer)

    date = datetime.date.today()
    store = HolidayDataStore(date, tracer)
    holiday_case = HolidayUseCase(store, tracer)

    case = PromotionUseCase(discounts=[holiday_case, user_case], tracer=tracer)

    no_exist = uuid.uuid4()
    result = case.promotion(no_exist)

    assert result.discounts[0].percentage == 10
def test_discount_max(user_store_mock, tracer):
    date = datetime.date.today()
    user = mock.MagicMock()
    type(user).birthday = mock.PropertyMock(return_value=date)
    user_store_mock.user.return_value = user
    user_case = UserUseCase(user_store_mock, tracer)

    date = datetime.date.today()
    store = HolidayDataStore(date, tracer)
    holiday_case = HolidayUseCase(store, tracer)

    case = PromotionUseCase(discounts=[holiday_case, user_case], tracer=tracer)

    result = case.promotion(user.id)

    assert result.discounts[0].percentage == 10
def test_discount_only_by_birthday(user_store_mock, tracer):
    date = datetime.date.today()
    user = mock.MagicMock()
    type(user).birthday = mock.PropertyMock(return_value=date)
    user_store_mock.query.return_value = user
    user_case = UserUseCase(user_store_mock, tracer)

    date = datetime.datetime.strptime("1970-12-01", "%Y-%m-%d").date()
    holiday_store = HolidayDataStore(date, tracer)
    holiday_case = HolidayUseCase(holiday_store, tracer)

    case = PromotionUseCase(discounts=[holiday_case, user_case], tracer=tracer)

    result = case.promotion(uuid.uuid4())

    assert result.discounts[0].percentage == 5
Beispiel #9
0
def test_server_create_user(database, tracer):
    user_store = UserDataStore(database, tracer)
    user_case = UserUseCase(user_store, tracer)

    order_store = OrderDataStore(database, tracer)
    order_case = OrderUseCase(order_store, tracer)

    date = datetime.datetime.strptime("1970-11-25", "%Y-%m-%d").date()
    holiday_store = HolidayDataStore(date, tracer)
    holiday_case = HolidayUseCase(holiday_store, tracer)

    balance_client = BalanceClient(settings.BALANCE_TOKEN)
    balance_case = BalanceUseCase(order_case, balance_client, tracer)

    authentication_case = AuthenticationUseCase(user_case, tracer)

    case = PromotionUseCase(discounts=[holiday_case, user_case], tracer=tracer)

    servicer = PromotionServicer(case, user_case, order_case, balance_case,
                                 authentication_case, tracer)

    date = datetime.date.today()
    birthday = Date(year=date.year, month=date.month, day=date.day)
    user_id = uuid.uuid4()
    request = CreateUserRequest(
        user_id=str(user_id).encode(),
        date_of_birth=birthday,
        identity="03303441965",
        email="*****@*****.**",
        name="User name",
        password="******",
    )
    result = servicer.CreateUser(request, None)

    assert database.query(User).one()
    assert result.date_of_birth == birthday
    assert result.identity == "03303441965"
    assert result.email == "*****@*****.**"
    assert result.name == "User name"
    assert result.user_id == str(user_id)