From a182cca5e9a339b1a754c7d2f09e516bd9c2be62 Mon Sep 17 00:00:00 2001 From: ShahRustam Date: Wed, 10 Jun 2020 11:07:19 +0300 Subject: [PATCH] tests fix (#3555) * replace timestamp with randint in tests * replace pop3 with imap --- .github/workflows/core.yml | 2 +- tests/commands/test_account.py | 10 +++++----- tests/commands/test_orgs.py | 8 ++++---- tests/commands/test_teams.py | 10 +++++----- tests/conftest.py | 29 +++++++++++++++-------------- 5 files changed, 30 insertions(+), 29 deletions(-) diff --git a/.github/workflows/core.yml b/.github/workflows/core.yml index c2e9547c..8332e944 100644 --- a/.github/workflows/core.yml +++ b/.github/workflows/core.yml @@ -30,7 +30,7 @@ jobs: env: TEST_EMAIL_LOGIN: ${{ secrets.TEST_EMAIL_LOGIN }} TEST_EMAIL_PASSWORD: ${{ secrets.TEST_EMAIL_PASSWORD }} - TEST_EMAIL_POP3_SERVER: ${{ secrets.TEST_EMAIL_POP3_SERVER }} + TEST_EMAIL_IMAP_SERVER: ${{ secrets.TEST_EMAIL_IMAP_SERVER }} run: | tox -e testcore diff --git a/tests/commands/test_account.py b/tests/commands/test_account.py index 221b724a..dff24f52 100644 --- a/tests/commands/test_account.py +++ b/tests/commands/test_account.py @@ -14,7 +14,7 @@ import json import os -import time +import random import pytest import requests @@ -32,7 +32,7 @@ from platformio.unpacker import FileUnpacker def test_account( clirunner, validate_cliresult, receive_email, isolated_pio_home, tmpdir_factory ): - username = "test-piocore-%s" % str(int(time.time() * 1000)) + username = "test-piocore-%s" % str(random.randint(0, 100000)) splited_email = os.environ.get("TEST_EMAIL_LOGIN").split("@") email = "%s+%s@%s" % (splited_email[0], username, splited_email[1]) firstname = "Test" @@ -191,10 +191,10 @@ def test_account( validate_cliresult(result) # pio account update - firstname = "First " + str(int(time.time() * 1000)) - lastname = "Last" + str(int(time.time() * 1000)) + firstname = "First " + str(random.randint(0, 100000)) + lastname = "Last" + str(random.randint(0, 100000)) - new_username = "username" + str(int(time.time() * 1000))[-5:] + new_username = "username" + str(random.randint(0, 100000)) new_email = "%s+new-%s@%s" % (splited_email[0], username, splited_email[1]) result = clirunner.invoke( cmd_account, diff --git a/tests/commands/test_orgs.py b/tests/commands/test_orgs.py index 3af38e83..d2001d0c 100644 --- a/tests/commands/test_orgs.py +++ b/tests/commands/test_orgs.py @@ -14,7 +14,7 @@ import json import os -import time +import random import pytest import requests @@ -28,7 +28,7 @@ from platformio.commands.org import cli as cmd_org reason="requires TEST_EMAIL_LOGIN, TEST_EMAIL_PASSWORD environ variables", ) # pylint:disable=too-many-arguments def test_org(clirunner, validate_cliresult, receive_email, isolated_pio_home): - username = "test-piocore-%s" % str(int(time.time() * 1000)) + username = "test-piocore-%s" % str(random.randint(0, 100000)) splited_email = os.environ.get("TEST_EMAIL_LOGIN").split("@") email = "%s+%s@%s" % (splited_email[0], username, splited_email[1]) firstname = "Test" @@ -71,7 +71,7 @@ def test_org(clirunner, validate_cliresult, receive_email, isolated_pio_home): result = clirunner.invoke(cmd_account, ["login", "-u", username, "-p", password],) validate_cliresult(result) - orgname = "testorg-piocore-%s" % str(int(time.time() * 1000)) + orgname = "testorg-piocore-%s" % str(random.randint(0, 100000)) display_name = "Test Org for PIO Core" second_username = "ivankravets" try: @@ -114,7 +114,7 @@ def test_org(clirunner, validate_cliresult, receive_email, isolated_pio_home): assert second_username not in result.output # pio org update - new_orgname = "neworg-piocore-%s" % str(int(time.time() * 1000)) + new_orgname = "neworg-piocore-%s" % str(random.randint(0, 100000)) new_display_name = "Test Org for PIO Core" result = clirunner.invoke( diff --git a/tests/commands/test_teams.py b/tests/commands/test_teams.py index 92d5226d..57085502 100644 --- a/tests/commands/test_teams.py +++ b/tests/commands/test_teams.py @@ -14,7 +14,7 @@ import json import os -import time +import random import pytest import requests @@ -29,7 +29,7 @@ from platformio.commands.team import cli as cmd_team reason="requires TEST_EMAIL_LOGIN, TEST_EMAIL_PASSWORD environ variables", ) # pylint:disable=too-many-arguments def test_teams(clirunner, validate_cliresult, receive_email, isolated_pio_home): - username = "test-piocore-%s" % str(int(time.time() * 1000)) + username = "test-piocore-%s" % str(random.randint(0, 100000)) splited_email = os.environ.get("TEST_EMAIL_LOGIN").split("@") email = "%s+%s@%s" % (splited_email[0], username, splited_email[1]) firstname = "Test" @@ -72,7 +72,7 @@ def test_teams(clirunner, validate_cliresult, receive_email, isolated_pio_home): result = clirunner.invoke(cmd_account, ["login", "-u", username, "-p", password],) validate_cliresult(result) - orgname = "testorg-piocore-%s" % str(int(time.time() * 1000)) + orgname = "testorg-piocore-%s" % str(random.randint(0, 100000)) display_name = "Test Org for PIO Core" # pio org create @@ -81,7 +81,7 @@ def test_teams(clirunner, validate_cliresult, receive_email, isolated_pio_home): ) validate_cliresult(result) - teamname = "test-" + str(int(time.time() * 1000)) + teamname = "test-" + str(random.randint(0, 100000)) team_description = "team for CI test" second_username = "ivankravets" try: @@ -128,7 +128,7 @@ def test_teams(clirunner, validate_cliresult, receive_email, isolated_pio_home): assert second_username not in result.output # pio team update - new_teamname = "new-" + str(int(time.time() * 1000)) + new_teamname = "new-" + str(random.randint(0, 100000)) newteam_description = "Updated Description" result = clirunner.invoke( cmd_team, diff --git a/tests/conftest.py b/tests/conftest.py index 542527b1..9fa3578b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -13,8 +13,8 @@ # limitations under the License. import email +import imaplib import os -import poplib import time import pytest @@ -63,9 +63,7 @@ def receive_email(): # pylint:disable=redefined-outer-name, too-many-locals def _receive_email(from_who): test_email = os.environ.get("TEST_EMAIL_LOGIN") test_password = os.environ.get("TEST_EMAIL_PASSWORD") - pop_server = os.environ.get("TEST_EMAIL_POP3_SERVER") or "pop.gmail.com" - if "gmail" in pop_server: - test_email = "recent:" + test_email + imap_server = os.environ.get("TEST_EMAIL_IMAP_SERVER") or "imap.gmail.com" def get_body(msg): if msg.is_multipart(): @@ -76,23 +74,26 @@ def receive_email(): # pylint:disable=redefined-outer-name, too-many-locals start_time = time.time() while not result: time.sleep(5) - server = poplib.POP3_SSL(pop_server) - server.user(test_email) - server.pass_(test_password) - _, mails, _ = server.list() - for index, _ in enumerate(mails): - _, lines, _ = server.retr(index + 1) - msg_content = b"\n".join(lines) + server = imaplib.IMAP4_SSL(imap_server) + server.login(test_email, test_password) + server.select("INBOX") + _, mails = server.search(None, "ALL") + for index in mails[0].split(): + _, data = server.fetch(index, "(RFC822)") msg = email.message_from_string( - msg_content.decode("ASCII", errors="surrogateescape") + data[0][1].decode("ASCII", errors="surrogateescape") ) if from_who not in msg.get("To"): continue - server.dele(index + 1) + if "gmail" in imap_server: + server.store(index, "+X-GM-LABELS", "\\Trash") + server.store(index, "+FLAGS", "\\Deleted") + server.expunge() result = get_body(msg).decode() if time.time() - start_time > 120: break - server.quit() + server.close() + server.logout() return result return _receive_email