You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ponymail.apache.org by se...@apache.org on 2022/01/26 23:40:39 UTC

[incubator-ponymail-foal] branch master updated: Start some proper tests

This is an automated email from the ASF dual-hosted git repository.

sebb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-ponymail-foal.git


The following commit(s) were added to refs/heads/master by this push:
     new e48a8f7  Start some proper tests
e48a8f7 is described below

commit e48a8f7f93779695e0bdf6dfbf1041829004140b
Author: Sebb <se...@apache.org>
AuthorDate: Wed Jan 26 23:40:03 2022 +0000

    Start some proper tests
---
 .github/workflows/integration-tests.yml | 14 ++++++
 test/requirements.txt                   |  3 +-
 test/test_integration.py                | 84 +++++++++++++++++++++++++++++++++
 3 files changed, 100 insertions(+), 1 deletion(-)

diff --git a/.github/workflows/integration-tests.yml b/.github/workflows/integration-tests.yml
index 3d68e3d..6d21907 100644
--- a/.github/workflows/integration-tests.yml
+++ b/.github/workflows/integration-tests.yml
@@ -49,9 +49,20 @@ jobs:
       run: |
         cd tools
         ./setup.py --defaults --devel
+        echo archiver.yaml 
         cat archiver.yaml
         cd ..
+        # add some oauth entries for use by the testauth plugin
+        cat >server/ponymail.yaml <<EOD
+          authoritative_domains:
+            - localhost
+          admins:
+            - admin@apache.org
+        EOD
+        echo server/ponymail.yaml 
         cat server/ponymail.yaml
+        # set up the testauth plugin
+        cp server/test/testauth.* server/endpoints/
     - name: Show Database
       run: |
         curl -sq "http://localhost:9200/_cat/indices?v"
@@ -80,6 +91,9 @@ jobs:
       if: always()
       run: |
         curl -sq 'http://localhost:8080/api/stats.lua?list=*&domain=ponymail.apache.org&d=gte=0d&emailsOnly'
+    - name: Test against database emails
+      run: |
+        python -m pytest -s test/test_integration.py
     - name: Shutdown
       if: always()
       run: |
diff --git a/test/requirements.txt b/test/requirements.txt
index 3065184..232e412 100644
--- a/test/requirements.txt
+++ b/test/requirements.txt
@@ -1,2 +1,3 @@
 # These items are needed for running tests
-mypy
\ No newline at end of file
+mypy
+requests
diff --git a/test/test_integration.py b/test/test_integration.py
new file mode 100644
index 0000000..6637300
--- /dev/null
+++ b/test/test_integration.py
@@ -0,0 +1,84 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import pytest
+import random
+import requests
+
+# Run as: python3 -m pytest [-s] test/test_integration.py
+
+API_BASE='http://localhost:8080/api'
+
+# Emulate how test auth is used by GUI
+def get_cookies(user='user'):
+    state=random.randint(
+        1000000000000000000,
+        2000000000000000000) # roughly equivalent to code in oauth.js
+    testauth='testauth'
+    res = requests.get(f"{API_BASE}/{testauth}?state={state}&redirect_uri=x&state={state}&key=ignored&user={user}",allow_redirects=False)
+    code = res.headers['Location'][1:]
+    res = requests.get(f"{API_BASE}/oauth.lua?key=ignored{code}&oauth_token={API_BASE}/{testauth}&state={state}")
+    cookies = res.cookies
+    print(res.text)
+    return cookies
+
+
+def test_lists():
+    jzon = requests.get(f"{API_BASE}/preferences").json()
+    # print(jzon)
+    lists = jzon['lists']
+    assert 'ponymail.apache.org' in lists
+    assert 'users' in lists['ponymail.apache.org']
+
+def test_public_stats():
+    jzon = requests.get(
+        f"{API_BASE}/stats.lua?list=users&domain=ponymail.apache.org&emailsOnly&d=gte=0d",
+    ).json()
+    assert jzon['firstYear'] == 2022
+    assert jzon['firstMonth'] == 1
+    assert jzon['lastYear'] == 2022
+    assert jzon['lastMonth'] == 1
+    assert jzon['hits'] == 6
+    for email in jzon['emails']:
+        assert email['list_raw'] == '<users.ponymail.apache.org>'
+        assert email['list'] == email['list_raw']
+        assert email['id'] == email['mid']
+        assert email['private'] == False
+    # Check we cannot see the private emails
+    jzon = requests.get(
+        f"{API_BASE}/stats.lua?list=users&domain=ponymail.apache.org&emailsOnly&d=2019-09"
+        ).json()
+    assert jzon['hits'] == 0
+
+def test_private_stats():
+    cookies = get_cookies()
+    # only fetch the private mail stats
+    jzon = requests.get(
+        f"{API_BASE}/stats.lua?list=users&domain=ponymail.apache.org&emailsOnly&d=2019-09",
+        cookies=cookies
+    ).json()
+    # The earlier mails are private
+    assert jzon['firstYear'] == 2019
+    assert jzon['firstMonth'] == 9
+    assert jzon['lastYear'] == 2022
+    assert jzon['lastMonth'] == 1
+    assert jzon['hits'] == 4
+    for email in jzon['emails']:
+        assert email['list_raw'] == '<users.ponymail.apache.org>'
+        assert email['list'] == email['list_raw']
+        assert email['id'] == email['mid']
+        assert email['private']