From 93ed653b7b7c2e2975a71936aabdce911e6a7b76 Mon Sep 17 00:00:00 2001
From: Jeremy Bowman <jbowman@edx.org>
Date: Fri, 6 Jul 2018 11:33:30 -0400
Subject: [PATCH] TE-2632 Fix site fixture domains for devstack bok-choy tests

---
 .../management/commands/update_fixtures.py    | 31 +++++++++++++
 .../util/tests/test_update_fixtures.py        | 43 +++++++++++++++++++
 pavelib/utils/test/suites/bokchoy_suite.py    | 21 ++++++++-
 3 files changed, 94 insertions(+), 1 deletion(-)
 create mode 100644 openedx/core/djangoapps/util/management/commands/update_fixtures.py
 create mode 100644 openedx/core/djangoapps/util/tests/test_update_fixtures.py

diff --git a/openedx/core/djangoapps/util/management/commands/update_fixtures.py b/openedx/core/djangoapps/util/management/commands/update_fixtures.py
new file mode 100644
index 00000000000..de6c4ba58e2
--- /dev/null
+++ b/openedx/core/djangoapps/util/management/commands/update_fixtures.py
@@ -0,0 +1,31 @@
+# -*- coding: utf-8 -*-
+"""
+Django management command to update the loaded test fixtures as necessary for
+the current test environment.  Currently just sets an appropriate domain for
+each Site fixture.
+"""
+from __future__ import absolute_import, division, print_function, unicode_literals
+
+import os
+
+from django.contrib.sites.models import Site
+from django.core.management.base import BaseCommand
+
+
+class Command(BaseCommand):
+    """
+    update_fixtures management command
+    """
+
+    help = "Update fixtures to match the current test environment."
+
+    def handle(self, *args, **options):
+        if 'BOK_CHOY_HOSTNAME' in os.environ:
+            # Fix the Site fixture domains so third party auth tests work correctly
+            host = os.environ['BOK_CHOY_HOSTNAME']
+            cms_port = os.environ['BOK_CHOY_CMS_PORT']
+            lms_port = os.environ['BOK_CHOY_LMS_PORT']
+            cms_domain = '{}:{}'.format(host, cms_port)
+            Site.objects.filter(name='cms').update(domain=cms_domain)
+            lms_domain = '{}:{}'.format(host, lms_port)
+            Site.objects.filter(name='lms').update(domain=lms_domain)
diff --git a/openedx/core/djangoapps/util/tests/test_update_fixtures.py b/openedx/core/djangoapps/util/tests/test_update_fixtures.py
new file mode 100644
index 00000000000..d29d9997a15
--- /dev/null
+++ b/openedx/core/djangoapps/util/tests/test_update_fixtures.py
@@ -0,0 +1,43 @@
+# -*- coding: utf-8 -*-
+"""
+Tests of the update_fixtures management command for bok-choy test database
+initialization.
+"""
+from __future__ import absolute_import, division, print_function, unicode_literals
+
+import os
+
+import pytest
+from django.contrib.sites.models import Site
+from django.core.management import call_command
+
+
+@pytest.fixture(scope='function')
+def sites(db):  # pylint: disable=unused-argument
+    Site.objects.create(name='cms', domain='localhost:8031')
+    Site.objects.create(name='lms', domain='localhost:8003')
+
+
+def test_localhost(db, monkeypatch, sites):  # pylint: disable=redefined-outer-name,unused-argument
+    monkeypatch.delitem(os.environ, 'BOK_CHOY_HOSTNAME', raising=False)
+    call_command('update_fixtures')
+    assert Site.objects.get(name='cms').domain == 'localhost:8031'
+    assert Site.objects.get(name='lms').domain == 'localhost:8003'
+
+
+def test_devstack_cms(db, monkeypatch, sites):  # pylint: disable=redefined-outer-name,unused-argument
+    monkeypatch.setitem(os.environ, 'BOK_CHOY_HOSTNAME', 'edx.devstack.cms')
+    monkeypatch.setitem(os.environ, 'BOK_CHOY_CMS_PORT', '18031')
+    monkeypatch.setitem(os.environ, 'BOK_CHOY_LMS_PORT', '18003')
+    call_command('update_fixtures')
+    assert Site.objects.get(name='cms').domain == 'edx.devstack.cms:18031'
+    assert Site.objects.get(name='lms').domain == 'edx.devstack.cms:18003'
+
+
+def test_devstack_lms(db, monkeypatch, sites):  # pylint: disable=redefined-outer-name,unused-argument
+    monkeypatch.setitem(os.environ, 'BOK_CHOY_HOSTNAME', 'edx.devstack.lms')
+    monkeypatch.setitem(os.environ, 'BOK_CHOY_CMS_PORT', '18031')
+    monkeypatch.setitem(os.environ, 'BOK_CHOY_LMS_PORT', '18003')
+    call_command('update_fixtures')
+    assert Site.objects.get(name='cms').domain == 'edx.devstack.lms:18031'
+    assert Site.objects.get(name='lms').domain == 'edx.devstack.lms:18003'
diff --git a/pavelib/utils/test/suites/bokchoy_suite.py b/pavelib/utils/test/suites/bokchoy_suite.py
index 7630284befb..c14030a206c 100644
--- a/pavelib/utils/test/suites/bokchoy_suite.py
+++ b/pavelib/utils/test/suites/bokchoy_suite.py
@@ -87,6 +87,24 @@ def load_courses(options):
         print colorize('blue', "--imports-dir not set, skipping import")
 
 
+@task
+@timed
+def update_fixtures():
+    """
+    Use the correct domain for the current test environment in each Site
+    fixture.  This currently differs between devstack cms, devstack lms,
+    and Jenkins.
+    """
+    msg = colorize('green', "Updating the Site fixture domains...")
+    print msg
+
+    sh(
+        " ./manage.py lms --settings={settings} update_fixtures".format(
+            settings=Env.SETTINGS
+        )
+    )
+
+
 @task
 @cmdopts([BOKCHOY_IMPORTS_DIR, BOKCHOY_IMPORTS_DIR_DEPR, PA11Y_FETCH_COURSE])
 @timed
@@ -142,7 +160,7 @@ def reset_test_database():
 
 
 @task
-@needs(['reset_test_database', 'clear_mongo', 'load_bok_choy_data', 'load_courses'])
+@needs(['reset_test_database', 'clear_mongo', 'load_bok_choy_data', 'load_courses', 'update_fixtures'])
 @might_call('start_servers')
 @cmdopts([BOKCHOY_FASTTEST], share_with=['start_servers'])
 @timed
@@ -236,6 +254,7 @@ class BokChoyTestSuite(TestSuite):
         else:
             # load data in db_fixtures
             load_bok_choy_data()  # pylint: disable=no-value-for-parameter
+            update_fixtures()
 
         msg = colorize('green', "Confirming servers have started...")
         print msg
-- 
GitLab