def test_module_http_response_token_auth_denied():

    actor_config = ActorConfig('http',
                               100,
                               1, {},
                               "",
                               disable_exception_handling=True)
    http = HTTPServer(actor_config,
                      resource={
                          ".*": {
                              "users": [],
                              "tokens": ["abc"],
                              "response": "OK {{uuid}}"
                          }
                      })

    http.pool.createQueue("outbox")
    http.pool.queue.outbox.disableFallThrough()
    http.start()

    r = requests.get('http://localhost:19283/outbox',
                     headers={"Authorization": "Token abcd"})
    http.stop()
    r.close()

    assert r.status_code == 403
def test_module_http_response_format_extra_params():

    actor_config = ActorConfig('http',
                               100,
                               1, {},
                               "",
                               disable_exception_handling=True)
    http = HTTPServer(actor_config,
                      resource={
                          ".*": {
                              "users": [],
                              "tokens": [],
                              "response": "12345 {{tmp.http.params.one}}"
                          }
                      },
                      htpasswd={})

    http.pool.createQueue("outbox")
    http.pool.queue.outbox.disableFallThrough()
    http.start()

    r = requests.get('http://localhost:19283?one=1')
    http.stop()
    r.close()

    assert r.status_code == 200
    assert r.text == "12345 1"
def test_module_http_response_default_so_reuseport():

    actor_config = ActorConfig('http', 100, 1, {}, "", disable_exception_handling=True)

    http1 = HTTPServer(
        actor_config,
        so_reuseport=True,
        resource={".*": {"users": [], "tokens": [], "response": "server1"}},
    )
    http1.pool.createQueue("outbox")
    http1.pool.queue.outbox.disableFallThrough()
    http1.start()

    http2 = HTTPServer(
        actor_config,
        so_reuseport=True,
        resource={".*": {"users": [], "tokens": [], "response": "server2"}},
    )
    http2.pool.createQueue("outbox")
    http2.pool.queue.outbox.disableFallThrough()
    http2.start()

    result = []

    # Check whether we actually hit both instances
    # In 10 times that should really be the case
    for _ in range(10):
        r = requests.get('http://localhost:19283')
        assert r.status_code == 200
        result.append(r.text)

    assert ["server1", "server2"] == sorted(set(result))
def test_module_http_response_user_auth_bad_header1():

    actor_config = ActorConfig('http',
                               100,
                               1, {},
                               "",
                               disable_exception_handling=True)
    http = HTTPServer(
        actor_config,
        resource={
            ".*": {
                "users": ["test"],
                "tokens": [],
                "response": "OK {{uuid}}"
            }
        },
        htpasswd={"test": "$apr1$rUKXjcuX$hqdIeoE2Q1Z/GMFhYsNO91"})

    http.pool.createQueue("outbox")
    http.pool.queue.outbox.disableFallThrough()
    http.start()

    r = requests.get('http://localhost:19283/outbox',
                     headers={"Authorization": "bad test"})
    http.stop()
    r.close()

    assert r.status_code == 400
def test_module_https_response_default():

    requests.packages.urllib3.disable_warnings()
    with open('/tmp/ssl.key', 'w') as f:
        f.write(SSL_KEY)

    with open('/tmp/ssl.cert', 'w') as f:
        f.write(SSL_CERT)

    actor_config = ActorConfig('http',
                               100,
                               1, {},
                               "",
                               disable_exception_handling=True)
    http = HTTPServer(actor_config,
                      ssl_key="/tmp/ssl.key",
                      ssl_cert="/tmp/ssl.cert")

    http.pool.createQueue("outbox")
    http.pool.queue.outbox.disableFallThrough()
    http.start()

    r = requests.get('https://localhost:19283', verify=False)
    http.stop()
    r.close()

    assert r.status_code == 200
    assert r.text.startswith("200 OK")

    unlink("/tmp/ssl.key")
    unlink("/tmp/ssl.cert")
def test_module_https_response_default():

    requests.packages.urllib3.disable_warnings()
    with open('/tmp/ssl.key', 'w') as f:
        f.write(SSL_KEY)

    with open('/tmp/ssl.cert', 'w') as f:
        f.write(SSL_CERT)

    actor_config = ActorConfig('http', 100, 1, {}, "", disable_exception_handling=True)
    http = HTTPServer(actor_config, ssl_key="/tmp/ssl.key", ssl_cert="/tmp/ssl.cert")

    http.pool.createQueue("outbox")
    http.pool.queue.outbox.disableFallThrough()
    http.start()

    r = requests.get('https://localhost:19283', verify=False)
    http.stop()
    r.close()

    assert r.status_code == 200
    assert r.text.startswith("200 OK")

    unlink("/tmp/ssl.key")
    unlink("/tmp/ssl.cert")
def test_module_http_response_user_auth_order():

    with TempFile("/tmp/htpasswd", "test:$apr1$rUKXjcuX$hqdIeoE2Q1Z/GMFhYsNO91"):

        actor_config = ActorConfig('http', 100, 1, {}, "", disable_exception_handling=True)
        http = HTTPServer(
            actor_config,
            resource={".*": {"users": ["test", "test2"], "tokens": [], "response": "OK {{uuid}}"}},
            htpasswd={"test": "$apr1$abc", "test2": "$apr1$rUKXjcuX$hqdIeoE2Q1Z/GMFhYsNO91"}
        )

        http.pool.createQueue("outbox")
        http.pool.queue.outbox.disableFallThrough()
        http.pool.queue._htpasswd.disableFallThrough()
        http.pool.queue._resource.disableFallThrough()
        http.start()
        http.pool.queue._htpasswd.put(Event({"path": "/tmp/htpasswd", "inotify_type": "WISHBONE_INIT"}))

        r = requests.get('http://localhost:19283/outbox', auth=("test", "test"))
        assert r.status_code == 200

        r = requests.get('http://localhost:19283/outbox', auth=("test2", "test"))
        assert r.status_code == 200

        http.stop()
        r.close()
def test_module_http_default_submit_event_outbox():

    actor_config = ActorConfig('http', 100, 1, {}, "", disable_exception_handling=True)
    http = HTTPServer(actor_config)

    http.pool.createQueue("outbox")
    http.pool.queue.outbox.disableFallThrough()
    http.start()

    r = requests.post('http://localhost:19283', data="hello")
    r.close()

    assert getter(http.pool.queue.outbox).get() == "hello"
    http.stop()
def test_module_http_response_non_existing():

    actor_config = ActorConfig('http', 100, 1, {}, "", disable_exception_handling=True)
    http = HTTPServer(actor_config)

    http.pool.createQueue("outbox")
    http.pool.queue.outbox.disableFallThrough()
    http.start()

    r = requests.get('http://localhost:19283/abc')
    http.stop()
    r.close()

    assert r.status_code == 404
def test_module_http_response_token_auth_denied():

    actor_config = ActorConfig('http', 100, 1, {}, "", disable_exception_handling=True)
    http = HTTPServer(
        actor_config,
        resource={".*": {"users": [], "tokens": ["abc"], "response": "OK {{uuid}}"}}
    )

    http.pool.createQueue("outbox")
    http.pool.queue.outbox.disableFallThrough()
    http.start()

    r = requests.get('http://localhost:19283/outbox', headers={"Authorization": "Token abcd"})
    http.stop()
    r.close()

    assert r.status_code == 403
def test_module_http_default_submit_event_outbox():

    actor_config = ActorConfig('http',
                               100,
                               1, {},
                               "",
                               disable_exception_handling=True)
    http = HTTPServer(actor_config)

    http.pool.createQueue("outbox")
    http.pool.queue.outbox.disableFallThrough()
    http.start()

    r = requests.post('http://localhost:19283', data="hello")
    r.close()

    assert getter(http.pool.queue.outbox).get() == "hello"
    http.stop()
def test_module_http_response_non_existing():

    actor_config = ActorConfig('http',
                               100,
                               1, {},
                               "",
                               disable_exception_handling=True)
    http = HTTPServer(actor_config)

    http.pool.createQueue("outbox")
    http.pool.queue.outbox.disableFallThrough()
    http.start()

    r = requests.get('http://localhost:19283/abc')
    http.stop()
    r.close()

    assert r.status_code == 404
def test_module_http_response_user_auth_bad_header1():

    actor_config = ActorConfig('http', 100, 1, {}, "", disable_exception_handling=True)
    http = HTTPServer(
        actor_config,
        resource={".*": {"users": ["test"], "tokens": [], "response": "OK {{uuid}}"}},
        htpasswd={"test": "$apr1$rUKXjcuX$hqdIeoE2Q1Z/GMFhYsNO91"}
    )

    http.pool.createQueue("outbox")
    http.pool.queue.outbox.disableFallThrough()
    http.start()

    r = requests.get('http://localhost:19283/outbox', headers={"Authorization": "bad test"})
    http.stop()
    r.close()

    assert r.status_code == 400
def test_module_http_response_default_so_reuseport():

    actor_config = ActorConfig('http',
                               100,
                               1, {},
                               "",
                               disable_exception_handling=True)

    http1 = HTTPServer(
        actor_config,
        so_reuseport=True,
        resource={".*": {
            "users": [],
            "tokens": [],
            "response": "server1"
        }},
    )
    http1.pool.createQueue("outbox")
    http1.pool.queue.outbox.disableFallThrough()
    http1.start()

    http2 = HTTPServer(
        actor_config,
        so_reuseport=True,
        resource={".*": {
            "users": [],
            "tokens": [],
            "response": "server2"
        }},
    )
    http2.pool.createQueue("outbox")
    http2.pool.queue.outbox.disableFallThrough()
    http2.start()

    result = []

    # Check whether we actually hit both instances
    # In 10 times that should really be the case
    for _ in range(10):
        r = requests.get('http://localhost:19283')
        assert r.status_code == 200
        result.append(r.text)

    assert ["server1", "server2"] == sorted(set(result))
def test_module_http_response_user_auth_order():

    with TempFile("/tmp/htpasswd",
                  "test:$apr1$rUKXjcuX$hqdIeoE2Q1Z/GMFhYsNO91"):

        actor_config = ActorConfig('http',
                                   100,
                                   1, {},
                                   "",
                                   disable_exception_handling=True)
        http = HTTPServer(actor_config,
                          resource={
                              ".*": {
                                  "users": ["test", "test2"],
                                  "tokens": [],
                                  "response": "OK {{uuid}}"
                              }
                          },
                          htpasswd={
                              "test": "$apr1$abc",
                              "test2": "$apr1$rUKXjcuX$hqdIeoE2Q1Z/GMFhYsNO91"
                          })

        http.pool.createQueue("outbox")
        http.pool.queue.outbox.disableFallThrough()
        http.pool.queue._htpasswd.disableFallThrough()
        http.pool.queue._resource.disableFallThrough()
        http.start()
        http.pool.queue._htpasswd.put(
            Event({
                "path": "/tmp/htpasswd",
                "inotify_type": "WISHBONE_INIT"
            }))

        r = requests.get('http://localhost:19283/outbox',
                         auth=("test", "test"))
        assert r.status_code == 200

        r = requests.get('http://localhost:19283/outbox',
                         auth=("test2", "test"))
        assert r.status_code == 200

        http.stop()
        r.close()
def test_module_http_response_format_extra_params():

    actor_config = ActorConfig('http', 100, 1, {}, "", disable_exception_handling=True)
    http = HTTPServer(
        actor_config,
        resource={".*": {"users": [], "tokens": [], "response": "12345 {{tmp.http.params.one}}"}},
        htpasswd={}
    )

    http.pool.createQueue("outbox")
    http.pool.queue.outbox.disableFallThrough()
    http.start()

    r = requests.get('http://localhost:19283?one=1')
    http.stop()
    r.close()

    assert r.status_code == 200
    assert r.text == "12345 1"