Skip to content

Commit

Permalink
Merge pull request #3446 from refinery-platform/ilan-gold/site_profil…
Browse files Browse the repository at this point in the history
…es-404

Update Site Profiles API
  • Loading branch information
ilan-gold committed Sep 12, 2019
2 parents 5e16bfc + f990b09 commit 0683eba
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 43 deletions.
84 changes: 59 additions & 25 deletions refinery/core/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1922,56 +1922,90 @@ def setUp(self, **kwargs):
view=SiteProfileAPIView.as_view()
)
self.current_site = Site.objects.get_current()
self.site_profile = SiteProfile.objects.create(
self.other_site = Site.objects.create(
domain='example.com', name='test'
)
self.site_profile_current = SiteProfile.objects.create(
site=self.current_site,
about_markdown='About the platform paragraph.',
intro_markdown='The refinery platform intro paragraph.',
twitter_username='Mock_twitter_name'
)
self.site_profile_other = SiteProfile.objects.create(
site=self.other_site,
about_markdown='About the other platform paragraph.',
intro_markdown='The other platform intro paragraph.',
twitter_username='Mock_twitter_name'
)
self.site_video_1 = SiteVideo.objects.create(
caption="Dashboard video",
site_profile=self.site_profile,
site_profile=self.site_profile_current,
source="YouTube",
source_id="yt_5tc"
)
self.site_video_2 = SiteVideo.objects.create(
caption="Analysis video",
site_profile=self.site_profile,
site_profile=self.site_profile_current,
source="YouTube",
source_id="yt_875"
)

username = password = "admin"
self.admin_user = User.objects.create_superuser(username, '', password)
self.get_request = self.factory.get(self.url_root)
self.get_request_current_true = self.factory.get(
self.url_root + '?current_site=True'
)
self.get_request_current_false = self.factory.get(
self.url_root + '?current_site=false'
)
self.get_request_current_foo = self.factory.get(
self.url_root + '?current_site=foo'
)
self.get_request_no_params = self.factory.get(self.url_root)

def test_get_returns_404_status_for_missing_site_profiles(self):
def test_get_returns_404_status_for_missing_current_site_profile(self):
SiteProfile.objects.all().delete()
get_response = self.view(self.get_request)
get_response = self.view(self.get_request_current_true)
self.assertEqual(get_response.status_code, 404)

def test_get_returns_200_status_for_anon_user(self):
get_response = self.view(self.get_request)
def test_get_returns_empty_list_for_no_params_no_profiles(self):
SiteProfile.objects.all().delete()
get_response = self.view(self.get_request_no_params)
self.assertEqual(get_response.status_code, 200)
self.assertEqual(len(get_response.data), 0)

def test_get_returns_site_profile(self):
get_response = self.view(self.get_request)
def test_get_returns_list_for_no_params(self):
get_response = self.view(self.get_request_no_params)
self.assertEqual(get_response.status_code, 200)
self.assertGreater(len(get_response.data), 0)

def test_get_returns_400_status_for_current_foo(self):
SiteProfile.objects.all().delete()
get_response = self.view(self.get_request_current_foo)
self.assertEqual(get_response.status_code, 200)

def test_get_current_profile_returns_200_status_for_anon_user(self):
get_response = self.view(self.get_request_current_true)
self.assertEqual(get_response.status_code, 200)

def test_get_current_profile_returns_site_profile(self):
get_response = self.view(self.get_request_current_true)
self.assertEqual(get_response.data.get('site'), self.current_site.id)

def test_get_returns_site_markdown_fields(self):
get_response = self.view(self.get_request)
get_response = self.view(self.get_request_current_true)
self.assertEqual(get_response.data.get('about_markdown'),
self.site_profile.about_markdown)
self.site_profile_current.about_markdown)
self.assertEqual(get_response.data.get('intro_markdown'),
self.site_profile.intro_markdown)
self.site_profile_current.intro_markdown)

def test_get_returns_twitter_username(self):
get_response = self.view(self.get_request)
get_response = self.view(self.get_request_current_true)
self.assertEqual(get_response.data.get('twitter_username'),
self.site_profile.twitter_username)
self.site_profile_current.twitter_username)

def test_get_returns_site_videos(self):
get_response = self.view(self.get_request)
get_response = self.view(self.get_request_current_true)
response_videos = [video.get('source_id') for video in
get_response.data.get('site_videos')]
self.assertItemsEqual(response_videos, [self.site_video_1.source_id,
Expand Down Expand Up @@ -2030,21 +2064,21 @@ def test_patch_updates_twitter_username(self):
def test_patch_updates_site_videos_lists_add(self):
site_video_1_data = {
"caption": self.site_video_1.caption,
"site_profile": self.site_profile.id,
"site_profile": self.site_profile_current.id,
"source": self.site_video_1.source,
"source_id": self.site_video_1.source_id,
"id": self.site_video_1.id
}
site_video_2_data = {
"caption": self.site_video_2.caption,
"site_profile": self.site_profile.id,
"site_profile": self.site_profile_current.id,
"source": self.site_video_2.source,
"source_id": self.site_video_2.source_id,
"id": self.site_video_2.id
}
site_video_3_data = {
"caption": "Video caption three.",
"site_profile": self.site_profile.id,
"site_profile": self.site_profile_current.id,
"source": "youtube",
"source_id": "yt_349v"
}
Expand All @@ -2056,14 +2090,14 @@ def test_patch_updates_site_videos_lists_add(self):
)
patch_request.user = self.admin_user
patch_response = self.view(patch_request)
self.site_profile.refresh_from_db()
self.assertEqual(len(self.site_profile.sitevideo_set.all()),
self.site_profile_current.refresh_from_db()
self.assertEqual(len(self.site_profile_current.sitevideo_set.all()),
len(patch_response.data.get('site_videos')))

def test_patch_updates_site_videos_lists_removal(self):
site_video_2_data = {
"caption": self.site_video_2.caption,
"site_profile": self.site_profile.id,
"site_profile": self.site_profile_current.id,
"source": self.site_video_2.source,
"source_id": self.site_video_2.source_id,
"id": self.site_video_2.id,
Expand All @@ -2074,15 +2108,15 @@ def test_patch_updates_site_videos_lists_removal(self):
)
patch_request.user = self.admin_user
patch_response = self.view(patch_request)
self.site_profile.refresh_from_db()
self.assertEqual(len(self.site_profile.sitevideo_set.all()),
self.site_profile_current.refresh_from_db()
self.assertEqual(len(self.site_profile_current.sitevideo_set.all()),
len(patch_response.data.get('site_videos')))

def test_patch_updates_site_videos_lists_updates_video_caption(self):
new_caption = 'New analysis video caption.'
site_video_1_data = {
"caption": new_caption,
"site_profile": self.site_profile.id,
"site_profile": self.site_profile_current.id,
"source": self.site_video_1.source,
"source_id": self.site_video_1.source_id,
"id": self.site_video_1.id
Expand Down
36 changes: 24 additions & 12 deletions refinery/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1434,18 +1434,30 @@ class SiteProfileAPIView(APIView):
http_method_names = ["get", "patch"]

def get(self, request):
try:
site_profile = SiteProfile.objects.get(
site=get_current_site(request)
)
except SiteProfile.DoesNotExist as e:
logger.error("Site profile for the current site does not exist.")
return HttpResponseNotFound(e)
except SiteProfile.MultipleObjectsReturned:
logger.error("Multiple site profiles for current site error.")
return HttpResponseServerError(e)

serializer = SiteProfileSerializer(site_profile)
"""
Returns the current profile ('current' param) or a list of profiles
:param request: API request
:return: serialized site profile(s)
"""
current_site = request.query_params.get('current_site', None)
if current_site == 'True':
try:
site_profile = SiteProfile.objects.get(
site=get_current_site(request)
)
except SiteProfile.DoesNotExist as e:
logger.error("Site profile for the current "
"site does not exist.")
return Response(str(e), status=status.HTTP_404_NOT_FOUND)
except SiteProfile.MultipleObjectsReturned as e:
logger.error("Multiple site profiles for "
"current site error.")
return Response(str(e), status=status.HTTP_400_BAD_REQUEST)

serializer = SiteProfileSerializer(site_profile)
else:
site_profiles = SiteProfile.objects.all()
serializer = SiteProfileSerializer(site_profiles, many=True)
return Response(serializer.data)

def patch(self, request):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('Anonymous user explores home page', function () {
cy.server();
cy.route({
method: 'GET',
url: '/api/v2/site_profiles/',
url: '/api/v2/site_profiles/?current_site=True',
response: '@site_profiles'
}).as('getSiteProfile');
cy.route({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Registered user explores home page', function () {
cy.server();
cy.route({
method: 'GET',
url: '/api/v2/site_profiles/',
url: '/api/v2/site_profiles/?current_site=True',
response: '@site_profiles'
}).as('getSiteProfile');
cy.route({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ describe('Common.service.siteProfileV2Service: unit tests', function () {
.expectGET(
refinerySettings.appRoot +
refinerySettings.refineryApiV2 +
'/site_profiles/'
'/site_profiles/?current_site=True'
).respond(200, siteProfile);

var results;
var promise = service.query().$promise
var promise = service.query({ current_site: 'True' }).$promise
.then(function (response) {
results = response;
});
Expand Down
2 changes: 1 addition & 1 deletion refinery/ui/source/js/home/directives/home.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
$httpBackend
.expectGET(
settings.appRoot +
settings.refineryApiV2 + '/site_profiles/'
settings.refineryApiV2 + '/site_profiles/?current_site=True'
).respond(200, []);

var scope = $rootScope.$new();
Expand Down
2 changes: 1 addition & 1 deletion refinery/ui/source/js/home/services/home-config-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* @memberOf refineryHome.getConfigs
**/
function getConfigs () {
var configs = siteProfileService.query();
var configs = siteProfileService.query({ current_site: 'True' });
configs.$promise.then(function (response) {
homeConfig.aboutMarkdown = response.about_markdown;
homeConfig.introMarkdown = response.intro_markdown;
Expand Down

0 comments on commit 0683eba

Please sign in to comment.