Esempio n. 1
0
def test_on_match_end(mocker, monkeypatch):
    fix_matcher_rand(mocker)
    delegate = UserStateDelegate()
    user = User.create("test1",
                       finding_genders="female",
                       gender="male",
                       points=30)
    partner = User.create("test2",
                          finding_genders="female,male",
                          gender="female",
                          points=30)
    time = datetime.datetime.utcnow()
    record = MatchingQueue(created_at=time, finding_genders="male,female")
    uc, pc = [UserContext(u) for u in [user, partner]]
    delegate.on_found_partner(uc, pc, record)

    delegate.on_speak(uc)
    delegate.on_speak(uc)
    delegate.on_speak(pc)

    delegate.on_match_ended(uc, pc)
    assert Match.get("test1") is None
    assert Match.get("test2") is None

    res = MatchHistory.get("test1")
    assert len(res) == 1
    assert res[0].founder_id == "test1"
    assert res[0].waiter_id == "test2"
    assert res[0].founder_speech_count == 2
    assert res[0].waiter_speech_count == 1
    assert res[0].founder_used_points == 10
    assert res[0].waiter_used_points == 0
    assert res[0].waited_at == time
    assert res[0].ended_by == "test1"
    assert res[0].started_at > time
Esempio n. 2
0
def test_payments_invalid_menu(monkeypatch):
    app = BetterTestApp(server.app)
    from chatty.libs import slack
    import stripe
    slackpost = MagicMock()
    mock_customer_create = MagicMock(return_value=MagicMock(id="customerid"))
    mock_charge_create = MagicMock(return_value=MagicMock(id="chargeid"))
    monkeypatch.setattr(slack, "post", slackpost)
    monkeypatch.setattr(stripe.Customer, "create", mock_customer_create)
    monkeypatch.setattr(stripe.Charge, "create", mock_charge_create)
    User.create("1878285869108664", fb_id="testfbid")
    uidenc = uidcrypt.encryptuid("1878285869108664")
    tok = tokener.gen_token("1878285869108664", tokener.NS_PAYMENT_SECOND)
    r = app.post_json("/" + uidenc + "/payments?access_token=" + tok,
                      {"token": "stripetoken",
                       "points": 500, "price": 5, "currency": "USD"},
                      status=400)
    assert slackpost.call_count == 0
    assert mock_customer_create.call_count == 0
    assert mock_charge_create.call_count == 0
    assert User.get("1878285869108664").points == 20

    assert PaymentSource.get("testfbid") is None
    assert len(Payment.get("testfbid")) == 0
    assert r.status == "400 Bad Request"
    assert "expiration" in r.json['text']
Esempio n. 3
0
def test_payments_faild(monkeypatch):
    from chatty.domain import payment
    from chatty.libs import slack
    import stripe
    mockpost = MagicMock()
    mock_customer_create = MagicMock(return_value=MagicMock(id="customerid"))
    mock_charge_create = MagicMock(return_value=MagicMock(id="chargeid1"))
    monkeypatch.setattr(slack, "post", mockpost)
    monkeypatch.setattr(stripe.Customer, "create", mock_customer_create)
    monkeypatch.setattr(stripe.Charge, "create", mock_charge_create)
    session = Session()
    User.create("test1", fb_id="testfbid")
    session.commit()
    session.close()

    def assert_no_effect():
        assert User.get("test1").points == 20
        assert PaymentSource.get("testfbid") is None
        assert len(Payment.get("testfbid")) == 0

    User.get("test1").points = 3999

    with pytest.raises(HTTPError) as e:
        payment.pay_from_form("test1", "stripetoken", 500, 495, "USD")
        assert "Limit Exceeded" in e.message
    assert mock_customer_create.call_count == 0
    assert mock_charge_create.call_count == 0
    assert_no_effect()

    with pytest.raises(HTTPError) as e:
        payment.pay_from_form("test1", "stripetoken", 500, 4, "USD")
        assert "menu might be changed" in e.message
    assert mock_customer_create.call_count == 0
    assert mock_charge_create.call_count == 0
    assert_no_effect()

    with pytest.raises(HTTPError) as e:
        payment.pay_from_form("test1", "stripetoken", 500, 495, "other")
        assert "menu might be changed" in e.message
    assert mock_customer_create.call_count == 0
    assert mock_charge_create.call_count == 0

    mock_customer_create.side_effect = stripe.error.CardError("e", {}, 400)
    with pytest.raises(HTTPError) as e:
        payment.pay_from_form("test1", "stripetoken", 500, 495, "USD")
        assert payment.MESSAGE_CONTACT_CREDIT in e.message
    assert_no_effect()

    mock_customer_create.side_effect = None
    mock_charge_create.side_effect = stripe.error.CardError("e", {}, 400)
    with pytest.raises(HTTPError) as e:
        payment.pay_from_form("test1", "stripetoken", 500, 495, "USD")
        assert payment.MESSAGE_CONTACT_CREDIT in e.message
    assert_no_effect()
Esempio n. 4
0
def test_payments_invalid_token(monkeypatch):
    app = BetterTestApp(server.app)
    User.create("1878285869108664", fb_id="testfbid")
    uidenc = uidcrypt.encryptuid("1878285869108664")
    tok = tokener.gen_token("1878285869108664", tokener.NS_PAYMENT_SECOND)
    r = app.post_json(
        "/" + uidenc + "/payments?access_token=" + "invalidtoken",
        {"token": "stripetoken",
         "points": 500, "price": 5, "currency": "USD"},
        status=400)
    assert User.get("1878285869108664").points == 20

    assert PaymentSource.get("testfbid") is None
    assert len(Payment.get("testfbid")) == 0
    assert r.status == "400 Bad Request"
    assert "expiration of token" in r.json['text']
Esempio n. 5
0
def test_payments(monkeypatch):
    from chatty.domain import payment
    from chatty.libs import slack
    import stripe
    mockpost = MagicMock()
    mock_customer_create = MagicMock(return_value=MagicMock(id="customerid"))
    mock_charge_create = MagicMock(return_value=MagicMock(id="chargeid1"))
    monkeypatch.setattr(slack, "post", mockpost)
    monkeypatch.setattr(stripe.Customer, "create", mock_customer_create)
    monkeypatch.setattr(stripe.Charge, "create", mock_charge_create)
    User.create("test1", fb_id="testfbid")
    assert User.get("test1").points == 20

    payment.pay_from_form("test1", "stripetoken", 500, 495, "USD")

    assert mock_customer_create.call_count == 1
    assert mock_charge_create.call_count == 1

    ps = PaymentSource.get("testfbid")
    assert ps.user_id == "testfbid"
    assert ps.stripe_id == "customerid"
    assert ps.created_at is not None
    payments = Payment.get("testfbid")
    assert len(payments) == 1
    assert payments[0].id == "chargeid1"
    assert payments[0].user_id == "testfbid"
    assert payments[0].stripe_user_id == "customerid"
    assert payments[0].points == 500
    assert payments[0].price == 495
    assert payments[0].currency == "usd"

    mock_charge_create().id = "chargeid2"
    payment.pay_from_form("test1", "stripetoken", 1500, 21383, "EGP")

    ps = PaymentSource.get("testfbid")
    assert ps.user_id == "testfbid"
    assert ps.stripe_id == "customerid"
    assert ps.created_at is not None
    payments = Payment.get("testfbid")
    assert len(payments) == 2
    assert payments[1].id == "chargeid2"
    assert payments[1].user_id == "testfbid"
    assert payments[1].stripe_user_id == "customerid"
    assert payments[1].points == 1500
    assert payments[1].price == 21383
    assert payments[1].currency == "egp"
Esempio n. 6
0
def test_matcher():
    assert Matcher().randomnum != Matcher().randomnum
    matcher = Matcher(randomnum=1)
    assert matcher.randomnum == 1
    user1 = User.create("u1", finding_genders="female,male", gender="male")
    user2 = User.create("u2", finding_genders="female,male", gender="female")
    user3 = User.create("u3", finding_genders="female", gender="female")
    user4 = User.create("u4", finding_genders="female", gender="male")
    cache = {'my_langs': set([Language.get("en_US")])}
    assert not matcher.should_match(user1, user2, cache, False)
    assert not matcher.should_match(user2, user3, cache, False)
    assert not matcher.should_match(user1, user4, cache, False)

    matcher.randomnum = 0
    assert matcher.should_match(user1, user2, cache, False)
    assert matcher.should_match(user2, user3, cache, False)
    assert not matcher.should_match(user1, user3, cache, False)
    assert not matcher.should_match(user1, user4, cache, False)
Esempio n. 7
0
def test_on_found_partner(mocker, monkeypatch):
    delegate = UserStateDelegate()
    fix_matcher_rand(mocker)

    user = User.create("test1",
                       finding_genders="male,female",
                       gender="male",
                       points=30)
    partner = User.create("test2",
                          finding_genders="male,female",
                          gender="male",
                          points=30)
    uc, pc = [UserContext(u) for u in [user, partner]]
    record = MatchingQueue(created_at=datetime.datetime.utcnow(),
                           finding_genders="male,female")
    delegate.on_found_partner(uc, pc, record)
    assert user.points == 30
    assert partner.points == 30
    assert Match.get("test1").founder_used_points == 0
    delegate.on_match_ended(uc, pc)

    user.finding_genders = "female"
    partner.gender = "female"
    delegate.on_found_partner(uc, pc, record)
    assert user.points == 20
    assert partner.points == 30
    assert Match.get("test1").founder_used_points == 10
    assert Match.get("test1").waiter_used_points == 0
    delegate.on_match_ended(uc, pc)

    delegate.on_found_partner(pc, uc, record)
    assert user.points == 20
    assert partner.points == 30
    record.finding_genders = "female"
    delegate.on_match_ended(uc, pc)

    delegate.on_found_partner(pc, uc, record)
    assert user.points == 10
    assert partner.points == 30
    assert Match.get("test1").founder_used_points == 0
    assert Match.get("test1").waiter_used_points == 10
Esempio n. 8
0
def test_on_try_search_on_banned(mocker, monkeypatch):
    fix_matcher_rand(mocker)
    delegate = UserStateDelegate()
    uc = UserContext(User.create("test1", "male,female", "male", 30))
    us = uc.status
    from chatty.util import fb

    mocksend = MagicMock()
    monkeypatch.setattr(fb, "send_data", mocksend)
    black_list.ban_user("test1")
    assert not delegate.on_try_search(us)
    assert mocksend.call_count == 1
    assert_message_is(mocksend.call_args_list[0][0][0], "test1", "suspended")
Esempio n. 9
0
def test_on_try_search(mocker, monkeypatch):
    fix_matcher_rand(mocker)
    delegate = UserStateDelegate()
    uc = UserContext(User.create("test1", "male,female", "male", 30))
    us = uc.status
    from chatty.util import fb

    mocksend = MagicMock()
    monkeypatch.setattr(fb, "send_data", mocksend)
    assert delegate.on_try_search(us)
    assert mocksend.call_count == 0

    us.user.points = 0
    assert delegate.on_try_search(us)
    assert mocksend.call_count == 0

    us.user.finding_genders = "female"
    assert not delegate.on_try_search(us)
    assert mocksend.call_count == 1

    assert_button(mocksend.call_args_list[0][0][0], "test1", "enough points",
                  [fb.web_button("", "/buy_points", "", "")])