Example #1
0
    def test_competition(self):
        """
        tests the html page for competition.
        """
        urlname = "arena.compete"
        # make a competition
        now = datetime.now()
        comp = Competition()
        comp.title = "test compo 123"
        comp.host = self.skiessi
        comp.theme = "test theme 123"
        comp.preview_theme = False
        comp.rules = "test rules 123"
        comp.preview_rules = False
        comp.start_date = now + timedelta(minutes=1)
        comp.submit_deadline = now + timedelta(minutes=2)
        comp.listening_party_start_date = comp.submit_deadline
        comp.listening_party_end_date = comp.listening_party_start_date
        comp.vote_period_length = 60  # seconds
        comp.chat_room = ChatRoom.objects.create()
        comp.save()

        # compo doesn't exist (404)
        response = self.client.get(reverse(urlname, args=[9]))  # bogus index
        self.assertEqual(response.status_code, 404)

        # logged out
        self.client.logout()
        response = self.client.get(reverse(urlname, args=[comp.id]))
        self.assertEqual(response.status_code, 200)

        # logged in
        self.assertEqual(self.client.login(username="******", password="******"), True)
        response = self.client.get(reverse(urlname, args=[comp.id]))
        self.assertEqual(response.status_code, 200)
Example #2
0
    def test_competition(self):
        """
        tests the html page for competition.
        """
        urlname = "arena.compete"
        # make a competition
        now = datetime.now()
        comp = Competition()
        comp.title = "test compo 123"
        comp.host = self.skiessi
        comp.theme = "test theme 123"
        comp.preview_theme = False
        comp.rules = "test rules 123"
        comp.preview_rules = False
        comp.start_date = now + timedelta(minutes=1)
        comp.submit_deadline = now + timedelta(minutes=2)
        comp.listening_party_start_date = comp.submit_deadline
        comp.listening_party_end_date = comp.listening_party_start_date
        comp.vote_period_length = 60  # seconds
        comp.chat_room = ChatRoom.objects.create()
        comp.save()

        # compo doesn't exist (404)
        response = self.client.get(reverse(urlname, args=[9]))  # bogus index
        self.assertEqual(response.status_code, 404)

        # logged out
        self.client.logout()
        response = self.client.get(reverse(urlname, args=[comp.id]))
        self.assertEqual(response.status_code, 200)

        # logged in
        self.assertEqual(
            self.client.login(username="******", password="******"), True)
        response = self.client.get(reverse(urlname, args=[comp.id]))
        self.assertEqual(response.status_code, 200)
Example #3
0
def create(request):
    prof = request.user.get_profile()
    err_msg = ""
    if request.method == 'POST':
        form = CreateCompetitionForm(request.POST)
        if form.is_valid() and request.user.is_authenticated():
            # create and save the Competition
            comp = Competition()
            comp.title = form.cleaned_data.get('title')
            comp.host = request.user

            comp.preview_theme = form.cleaned_data.get('preview_theme', False)
            if form.cleaned_data.get('have_theme', False):
                comp.theme = form.cleaned_data.get('theme', '')

            comp.preview_rules = form.cleaned_data.get('preview_rules', False)
            if form.cleaned_data.get('have_rules', False):
                comp.rules = form.cleaned_data.get('rules', '')

            tz_offset = form.cleaned_data.get('tz_offset')
            tz_delta = timedelta(hours=tz_offset)

            comp.start_date = form.cleaned_data.get('start_date') + tz_delta
            comp.submit_deadline = form.cleaned_data.get(
                'submission_deadline_date') + tz_delta

            # calculate vote deadline
            quantity = int(form.cleaned_data.get('vote_time_quantity'))
            quantifier = int(form.cleaned_data.get('vote_time_measurement'))

            comp.vote_period_length = quantity * TimeUnit.seconds_multiplier[
                quantifier]
            vote_period_delta = timedelta(seconds=comp.vote_period_length)

            comp.have_listening_party = form.cleaned_data.get(
                'have_listening_party', True)
            if comp.have_listening_party:
                if form.cleaned_data.get('party_immediately'):
                    comp.listening_party_start_date = comp.submit_deadline
                else:
                    comp.listening_party_start_date = form.cleaned_data.get(
                        'listening_party_date') + tz_delta
                # initialize end date to start date. we make modifications to it
                # when entries are submitted.
                comp.listening_party_end_date = comp.listening_party_start_date

                # this changes whenever listening_party_end_date changes.
                comp.vote_deadline = comp.listening_party_end_date + vote_period_delta
            else:
                comp.vote_deadline = comp.submit_deadline + vote_period_delta

            # create a chatroom for it
            chatroom = ChatRoom()
            chatroom.permission_type = ChatRoom.OPEN
            # open the chat room an hour before the competition
            chatroom.start_date = comp.start_date - timedelta(hours=1)
            # chat room is open an hour before and after competition
            chatroom.end_date = comp.listening_party_end_date + timedelta(
                hours=1)
            chatroom.save()
            comp.chat_room = chatroom

            comp.save()

            # automatically bookmark it
            prof.competitions_bookmarked.add(comp)
            prof.save()

            return HttpResponseRedirect(reverse("arena.home"))
    else:
        initial = {
            'have_theme': True,
            'have_rules': True,
            'preview_rules': True,
            'have_listening_party': True,
            'party_immediately': True,
            'vote_time_quantity': 1,
            'vote_time_measurement': TimeUnit.WEEKS,
        }
        form = CreateCompetitionForm(initial=initial)

    return render_to_response('arena/create.html',
                              locals(),
                              context_instance=RequestContext(request))
Example #4
0
def create(request):
    prof = request.user.get_profile()
    err_msg = ""
    if request.method == 'POST':
        form = CreateCompetitionForm(request.POST)
        if form.is_valid() and request.user.is_authenticated():
            # create and save the Competition
            comp = Competition()
            comp.title = form.cleaned_data.get('title')
            comp.host = request.user

            comp.preview_theme = form.cleaned_data.get('preview_theme', False)
            if form.cleaned_data.get('have_theme', False):
                comp.theme = form.cleaned_data.get('theme', '')

            comp.preview_rules = form.cleaned_data.get('preview_rules', False)
            if form.cleaned_data.get('have_rules', False):
                comp.rules = form.cleaned_data.get('rules', '')

            tz_offset = form.cleaned_data.get('tz_offset')
            tz_delta = timedelta(hours=tz_offset)

            comp.start_date = form.cleaned_data.get('start_date') + tz_delta
            comp.submit_deadline = form.cleaned_data.get('submission_deadline_date') + tz_delta

            # calculate vote deadline 
            quantity = int(form.cleaned_data.get('vote_time_quantity'))
            quantifier = int(form.cleaned_data.get('vote_time_measurement'))

            comp.vote_period_length = quantity * TimeUnit.seconds_multiplier[quantifier]
            vote_period_delta = timedelta(seconds=comp.vote_period_length)

            comp.have_listening_party = form.cleaned_data.get('have_listening_party', True)
            if comp.have_listening_party:
                if form.cleaned_data.get('party_immediately'):
                    comp.listening_party_start_date = comp.submit_deadline
                else:
                    comp.listening_party_start_date = form.cleaned_data.get('listening_party_date') + tz_delta
                # initialize end date to start date. we make modifications to it
                # when entries are submitted.
                comp.listening_party_end_date = comp.listening_party_start_date

                # this changes whenever listening_party_end_date changes.
                comp.vote_deadline = comp.listening_party_end_date + vote_period_delta
            else:
                comp.vote_deadline = comp.submit_deadline + vote_period_delta

            # create a chatroom for it
            chatroom = ChatRoom()
            chatroom.permission_type = ChatRoom.OPEN
            # open the chat room an hour before the competition
            chatroom.start_date = comp.start_date - timedelta(hours=1)
            # chat room is open an hour before and after competition
            chatroom.end_date = comp.listening_party_end_date + timedelta(hours=1)
            chatroom.save()
            comp.chat_room = chatroom;

            comp.save()


            # automatically bookmark it
            prof.competitions_bookmarked.add(comp)
            prof.save()

            return HttpResponseRedirect(reverse("arena.home"))
    else:
        initial = {
            'have_theme': True,
            'have_rules': True,
            'preview_rules': True,
            'have_listening_party': True,
            'party_immediately': True,
            'vote_time_quantity': 1,
            'vote_time_measurement': TimeUnit.WEEKS,
        }
        form = CreateCompetitionForm(initial=initial)
    
    return render_to_response('arena/create.html', locals(),
        context_instance=RequestContext(request))
Example #5
0
    def test_ajax_available(self):
        """
        tests available, owned, bookmark, and unbookmark
        """
        url = reverse("arena.ajax_available")
        # should be no competitions available
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(len(data["compos"]), 0)

        # make a competition
        now = datetime.now()
        comp = Competition()
        comp.title = "test compo 123"
        comp.host = self.skiessi
        comp.theme = "test theme 123"
        comp.preview_theme = False
        comp.rules = "test rules 123"
        comp.preview_rules = True
        comp.start_date = now + timedelta(minutes=1)
        comp.submit_deadline = now + timedelta(minutes=2)
        comp.listening_party_start_date = comp.submit_deadline
        comp.listening_party_end_date = comp.listening_party_start_date
        comp.vote_period_length = 60  # seconds
        comp.chat_room = ChatRoom.objects.create()
        comp.save()

        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(len(data["compos"]), 1)
        self.assertEqual(data["compos"][0]["title"], "test compo 123")
        # compo can't have theme yet because it didn't start yet
        self.assertEqual(data["compos"][0].has_key("theme"), False)
        # rules are on preview though
        self.assertEqual(data["compos"][0]["rules"], "test rules 123")

        # log in and bookmark the competition
        self.assertEqual(self.client.login(username="******", password="******"), True)
        bookmark_url = reverse("arena.ajax_bookmark", args=[comp.id])
        response = self.client.get(bookmark_url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data["success"], True)

        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(len(data["compos"]), 0)

        # unbookmark it
        unbookmark_url = reverse("arena.ajax_unbookmark", args=[comp.id])
        response = self.client.get(unbookmark_url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data["success"], True)

        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(len(data["compos"]), 1)

        # delete it
        comp.delete()

        # make sure paging works
        howMany = settings.ITEMS_PER_PAGE * 3  # should be 3 pages
        for _ in range(howMany):
            now = datetime.now()
            comp = Competition()
            comp.title = "title"
            comp.host = self.superjoe
            comp.theme = "theme"
            comp.preview_theme = True
            comp.rules = "rules"
            comp.preview_rules = True
            comp.start_date = now - timedelta(minutes=1)
            comp.submit_deadline = now + timedelta(minutes=1)
            comp.listening_party_start_date = comp.submit_deadline
            comp.listening_party_end_date = comp.listening_party_start_date
            comp.vote_period_length = 60  # seconds
            comp.chat_room = ChatRoom.objects.create()
            comp.save()

        # default first page
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(len(data["compos"]), settings.ITEMS_PER_PAGE)
        self.assertEqual(data["compos"][0]["theme"], "theme")
        self.assertEqual(data["compos"][0]["rules"], "rules")
        self.assertEqual(data["compos"][0]["title"], "title")
        self.assertEqual(data["page_count"], 3)
        self.assertEqual(data["page_number"], 1)

        # get second page
        response = self.client.get(url, {"page": 2})
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(len(data["compos"]), settings.ITEMS_PER_PAGE)
        self.assertEqual(data["page_count"], 3)
        self.assertEqual(data["page_number"], 2)
Example #6
0
    def test_ajax_competition(self):
        """
        this tests:
            ajax_compo 
            ajax_vote
            ajax_unvote
            ajax_submit_entry
        """
        urlname_compo = "arena.ajax_compo"

        # request a compo that doesn't exist
        response = self.client.get(reverse(urlname_compo, args=[4]))
        self.assertEqual(response.status_code, 404)

        # create a competition that hasn't started yet
        now = datetime.now()
        comp = Competition()
        comp.title = "test compo 123"
        comp.host = self.skiessi
        comp.theme = "test theme 123"
        comp.preview_theme = False
        comp.rules = "test rules 123"
        comp.preview_rules = False
        comp.start_date = now + timedelta(minutes=1)
        comp.submit_deadline = now + timedelta(minutes=2)
        comp.listening_party_start_date = comp.submit_deadline
        comp.listening_party_end_date = comp.listening_party_start_date
        comp.vote_period_length = 60  # seconds
        comp.chat_room = ChatRoom.objects.create()
        comp.save()

        # check that the request works
        response = self.client.get(reverse(urlname_compo, args=[comp.id]))
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data["user"]["is_authenticated"], False)
        self.assertEqual(data.has_key("votes"), False)
        self.assertEqual(data["compo"].has_key("theme"), False)
        self.assertEqual(data["compo"].has_key("rules"), False)
        self.assertEqual(data["compo"]["title"], "test compo 123")
        self.assertEqual(data["party"]["buffer_time"], settings.LISTENING_PARTY_BUFFER_TIME)

        # adjust the date so that it is started
        comp.start_date = now - timedelta(minutes=1)
        comp.save()

        # make sure we can see the rules and theme now
        response = self.client.get(reverse(urlname_compo, args=[comp.id]))
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data["compo"]["theme"], "test theme 123")
        self.assertEqual(data["compo"]["rules"], "test rules 123")

        ########## submitting entries ###############
        now = datetime.now()
        url_submit = reverse("arena.ajax_submit_entry")
        # try to submit to a bogus competition
        self.assertEqual(self.client.login(username="******", password="******"), True)
        mp3file = open(absolute("fixtures/silence10s-flstudio-notags.mp3"), "rb")
        response = self.client.post(
            url_submit,
            {
                "compo": 90,  # bogus number
                "entry-file-mp3": mp3file,
                #'entry-file-source': sourcefile,
                "entry-title": "test title 123",
                "entry-comments": "test comments 123",
            },
        )
        mp3file.close()
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data["success"], False)
        self.assertEqual(data["reason"], design.competition_not_found)
        self.assertEqual(Entry.objects.count(), 0)
        self.assertEqual(Song.objects.count(), 0)

        # try to submit to a not started competition
        comp.start_date = now + timedelta(minutes=1)
        comp.save()
        mp3file = open(absolute("fixtures/silence10s-flstudio-notags.mp3"), "rb")
        sourcefile = open(absolute("fixtures/blank.flp"), "rb")
        response = self.client.post(
            url_submit,
            {
                "compo": comp.id,
                "entry-file-mp3": mp3file,
                "entry-file-source": sourcefile,
                "entry-title": "test title 123",
                "entry-comments": "test comments 123",
            },
        )
        mp3file.close()
        sourcefile.close()
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data["success"], False)
        self.assertEqual(data["reason"], design.competition_not_started)
        self.assertEqual(Entry.objects.count(), 0)
        self.assertEqual(Song.objects.count(), 0)

        # try to submit after the deadline
        comp.start_date = now - timedelta(minutes=1)
        comp.submit_deadline = now - timedelta(minutes=1)
        comp.save()
        mp3file = open(absolute("fixtures/silence10s-flstudio-notags.mp3"), "rb")
        sourcefile = open(absolute("fixtures/blank.flp"), "rb")
        response = self.client.post(
            url_submit,
            {
                "compo": comp.id,
                "entry-file-mp3": mp3file,
                "entry-file-source": sourcefile,
                "entry-title": "test title 123",
                "entry-comments": "test comments 123",
            },
        )
        mp3file.close()
        sourcefile.close()
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data["success"], False)
        self.assertEqual(data["reason"], design.past_submission_deadline)
        self.assertEqual(Entry.objects.count(), 0)
        self.assertEqual(Song.objects.count(), 0)

        # try to submit logged out
        comp.start_date = now - timedelta(minutes=1)
        comp.submit_deadline = now + timedelta(minutes=1)
        comp.save()
        self.client.logout()
        mp3file = open(absolute("fixtures/silence10s-flstudio-notags.mp3"), "rb")
        sourcefile = open(absolute("fixtures/blank.flp"), "rb")
        response = self.client.post(
            url_submit,
            {
                "compo": comp.id,
                "entry-file-mp3": mp3file,
                "entry-file-source": sourcefile,
                "entry-title": "test title 123",
                "entry-comments": "test comments 123",
            },
        )
        mp3file.close()
        sourcefile.close()
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data["success"], False)
        self.assertEqual(data["reason"], design.not_authenticated)
        self.assertEqual(Entry.objects.count(), 0)
        self.assertEqual(Song.objects.count(), 0)

        # try to submit without providing mp3 file
        self.assertEqual(self.client.login(username="******", password="******"), True)
        sourcefile = open(absolute("fixtures/blank.flp"), "rb")
        response = self.client.post(
            url_submit,
            {
                "compo": comp.id,
                #'entry-file-mp3': mp3file,
                "entry-file-source": sourcefile,
                "entry-title": "test title 123",
                "entry-comments": "test comments 123",
            },
        )
        sourcefile.close()
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data["success"], False)
        self.assertEqual(data["reason"], design.mp3_required)
        self.assertEqual(Entry.objects.count(), 0)
        self.assertEqual(Song.objects.count(), 0)

        # submit a file that is too big
        # create a temp file that is > settings.FILE_UPLOAD_SIZE_CAP
        handle = tempfile.NamedTemporaryFile(suffix="mp3", delete=False)
        self.dirtyFiles.append(handle.name)
        bytesWritten = 0
        bufferSize = 1024 * 1024
        while bytesWritten <= settings.FILE_UPLOAD_SIZE_CAP:
            handle.write(" " * bufferSize)
            bytesWritten += bufferSize
        handle.close()
        mp3file = open(handle.name, "rb")
        sourcefile = open(absolute("fixtures/blank.flp"), "rb")
        response = self.client.post(
            url_submit,
            {
                "compo": comp.id,
                "entry-file-mp3": mp3file,
                "entry-file-source": sourcefile,
                "entry-title": "test title 123",
                "entry-comments": "test comments 123",
            },
        )
        mp3file.close()
        sourcefile.close()
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data["success"], False)
        self.assertEqual(data["reason"], design.mp3_too_big)
        self.assertEqual(Entry.objects.count(), 0)
        self.assertEqual(Song.objects.count(), 0)
        os.remove(handle.name)

        # submit an mp3 file that is too long
        mp3file = open(absolute("fixtures/silence11m-notags.mp3"), "rb")
        sourcefile = open(absolute("fixtures/blank.flp"), "rb")
        response = self.client.post(
            url_submit,
            {
                "compo": comp.id,
                "entry-file-mp3": mp3file,
                "entry-file-source": sourcefile,
                "entry-title": "test title 123",
                "entry-comments": "test comments 123",
            },
        )
        mp3file.close()
        sourcefile.close()
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data["success"], False)
        self.assertEqual(data["reason"], design.song_too_long)
        self.assertEqual(Entry.objects.count(), 0)
        self.assertEqual(Song.objects.count(), 0)

        # submit an ogg file instead of mp3
        mp3file = open(absolute("fixtures/silence10s-flstudio-notags.ogg"), "rb")
        sourcefile = open(absolute("fixtures/blank.flp"), "rb")
        response = self.client.post(
            url_submit,
            {
                "compo": comp.id,
                "entry-file-mp3": mp3file,
                "entry-file-source": sourcefile,
                "entry-title": "test title 123",
                "entry-comments": "test comments 123",
            },
        )
        mp3file.close()
        sourcefile.close()
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data["success"], False)
        self.assertEqual(data["reason"] in (design.sketchy_mp3_file, design.invalid_mp3_file), True)
        self.assertEqual(Entry.objects.count(), 0)
        self.assertEqual(Song.objects.count(), 0)

        # submit a good file, don't provide source or comments
        # flstudio exported, no tags
        self.assertEqual(self.client.login(username="******", password="******"), True)
        mp3file = open(absolute("fixtures/silence10s-flstudio-notags.mp3"), "rb")
        response = self.client.post(
            url_submit,
            {
                "compo": comp.id,
                "entry-file-mp3": mp3file,
                #'entry-file-source': sourcefile,
                "entry-title": "superjoe title 123",
                #'entry-comments': "test comments 123",
            },
        )
        mp3file.close()
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data["success"], True)
        self.assertEqual(Entry.objects.count(), 1)
        self.assertEqual(Song.objects.count(), 1)
        entry = Entry.objects.filter(owner=self.superjoe)[0]
        # TODO: use storage to ensure path exists
        # self.assertEqual(os.path.exists(os.path.join(settings.MEDIA_ROOT, entry.song.mp3_file)), True)
        # self.assertEqual(os.path.exists(os.path.join(settings.MEDIA_ROOT, entry.song.waveform_img)), True)
        self.assertEqual(entry.song.band.id, self.superjoe.get_profile().solo_band.id)
        self.assertEqual(entry.song.title, "superjoe title 123")
        version = ProjectVersion.objects.order_by("-pk")[0]
        self.assertNotEqual(version.song.comment_node, None)
        self.assertEqual(version.song.is_open_for_comments, True)

        # submit a good file, provide source and comments. open source.
        # flstudio exported, tags
        self.assertEqual(self.client.login(username="******", password="******"), True)
        mp3file = open(absolute("fixtures/silence10s-flstudio-tags.mp3"), "rb")
        sourcefile = open(absolute("fixtures/blank.flp"), "rb")
        response = self.client.post(
            url_submit,
            {
                "compo": comp.id,
                "entry-file-mp3": mp3file,
                "entry-file-source": sourcefile,
                "entry-title": "just64helpin title 123",
                "entry-comments": "just64helpin comments 123",
                "entry-open-source": "1",
            },
        )
        mp3file.close()
        sourcefile.close()
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data["success"], True)
        self.assertEqual(Entry.objects.count(), 2)
        self.assertEqual(Song.objects.count(), 2)
        entry = Entry.objects.filter(owner=self.just64helpin)[0]
        # TODO: use storage instead of local filesystem
        # self.assertEqual(os.path.exists(os.path.join(settings.MEDIA_ROOT, entry.song.mp3_file)), True)
        # self.assertEqual(os.path.exists(os.path.join(settings.MEDIA_ROOT, entry.song.waveform_img)), True)
        self.assertEqual(entry.song.band.id, self.just64helpin.get_profile().solo_band.id)
        self.assertEqual(entry.song.title, "just64helpin title 123")
        self.assertEqual(SongCommentNode.objects.count(), 2)
        self.assertEqual(entry.song.comment_node.content, "just64helpin comments 123")
        self.assertEqual(entry.song.is_open_source, True)
        version = ProjectVersion.objects.order_by("-pk")[0]
        self.assertEqual(version.song.comment_node.content, "just64helpin comments 123")
        self.assertEqual(version.song.is_open_for_comments, True)

        # submit a good file, provide source but no comments. not open source.
        # no tags, vbr
        self.assertEqual(self.client.login(username="******", password="******"), True)
        mp3file = open(absolute("fixtures/silence10s-notags-vbr.mp3"), "rb")
        sourcefile = open(absolute("fixtures/blank.flp"), "rb")
        response = self.client.post(
            url_submit,
            {
                "compo": comp.id,
                "entry-file-mp3": mp3file,
                "entry-file-source": sourcefile,
                "entry-title": "skiessi title 123",
                #'entry-comments': "skiessi comments 123",
                #'entry-open-source': "1",
            },
        )
        mp3file.close()
        sourcefile.close()
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data["success"], True)
        self.assertEqual(Entry.objects.count(), 3)
        self.assertEqual(Song.objects.count(), 3)
        entry = Entry.objects.filter(owner=self.skiessi)[0]
        # TODO: use storage instead of local file system
        # self.assertEqual(os.path.exists(os.path.join(settings.MEDIA_ROOT, entry.song.mp3_file)), True)
        # self.assertEqual(os.path.exists(os.path.join(settings.MEDIA_ROOT, entry.song.waveform_img)), True)
        self.assertEqual(entry.song.band.id, self.skiessi.get_profile().solo_band.id)
        self.assertEqual(entry.song.title, "skiessi title 123")
        self.assertEqual(entry.song.is_open_source, False)
        version = ProjectVersion.objects.order_by("-pk")[0]
        self.assertNotEqual(version.song.comment_node, None)
        self.assertEqual(version.song.is_open_for_comments, True)

        # resubmit
        mp3file = open(absolute("fixtures/silence10s-notags-vbr.mp3"), "rb")
        sourcefile = open(absolute("fixtures/blank.flp"), "rb")
        response = self.client.post(
            url_submit,
            {
                "compo": comp.id,
                "entry-file-mp3": mp3file,
                "entry-file-source": sourcefile,
                "entry-title": "skiessi title 123 v2",
                "entry-comments": "skiessi comments 123 v2",
            },
        )
        mp3file.close()
        sourcefile.close()
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data["success"], True)
        self.assertEqual(Entry.objects.count(), 3)
        self.assertEqual(Song.objects.count(), 4)  # new version
        self.assertEqual(SongCommentNode.objects.count(), 4)
        entry = Entry.objects.filter(owner=self.skiessi)[0]
        # TODO: use storage instead of local file system
        # self.assertEqual(os.path.exists(os.path.join(settings.MEDIA_ROOT, entry.song.mp3_file)), True)
        # self.assertEqual(os.path.exists(os.path.join(settings.MEDIA_ROOT, entry.song.waveform_img)), True)
        self.assertEqual(entry.song.band.id, self.skiessi.get_profile().solo_band.id)
        self.assertEqual(entry.song.title, "skiessi title 123 v2")
        self.assertEqual(entry.song.comment_node.content, "skiessi comments 123 v2")
        version = ProjectVersion.objects.order_by("-pk")[0]
        self.assertEqual(version.song.comment_node.content, "skiessi comments 123 v2")
        self.assertEqual(version.version, 2)
        self.assertEqual(version.song.is_open_for_comments, True)

        # check ajax_compo to make sure it gets the entry list right
        response = self.client.get(reverse(urlname_compo, args=[comp.id]))
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data["user"]["is_authenticated"], True)
        self.assertEqual(len(data["entries"]), 3)
        self.assertEqual(data["entries"][0]["owner"]["username"], "superjoe")
        self.assertEqual(data["entries"][1]["owner"]["username"], "just64helpin")
        self.assertEqual(data["entries"][2]["owner"]["username"], "skiessi")

        ############# voting ################
        urlname_vote = "arena.ajax_vote"
        urlname_unvote = "arena.ajax_unvote"
        comp.have_listening_party = False
        comp.submit_deadline = now - timedelta(minutes=1)
        comp.vote_deadline = now + timedelta(minutes=1)
        comp.save()

        ############ voting ###############
        # try to vote logged out
        self.client.logout()
        response = self.client.get(reverse(urlname_vote, args=[entry.id]))
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data["success"], False)
        self.assertEqual(data["reason"], design.not_authenticated)
        self.assertEqual(ThumbsUp.objects.count(), 0)

        # 2 people vote for superjoe, make sure he has 2 votes
        # skiessi vote for superjoe
        self.assertEqual(self.client.login(username="******", password="******"), True)
        entry = Entry.objects.filter(owner=self.superjoe)[0]
        response = self.client.get(reverse(urlname_vote, args=[entry.id]))
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data["success"], True)
        self.assertEqual(ThumbsUp.objects.filter(entry=entry).count(), 1)

        # just64helpin vote for superjoe
        self.assertEqual(self.client.login(username="******", password="******"), True)
        response = self.client.get(reverse(urlname_vote, args=[entry.id]))
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data["success"], True)
        self.assertEqual(ThumbsUp.objects.filter(entry=entry).count(), 2)

        # make sure his vote count is decremented
        response = self.client.get(reverse(urlname_compo, args=[comp.id]))
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data["votes"]["max"], 1)
        self.assertEqual(len(data["votes"]["used"]), 1)
        self.assertEqual(data["votes"]["used"][0]["entry"], entry.id)
        self.assertEqual(data["votes"]["left"], 0)

        # vote for yourself (invalid)
        self.assertEqual(self.client.login(username="******", password="******"), True)
        response = self.client.get(reverse(urlname_vote, args=[entry.id]))
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data["success"], False)
        self.assertEqual(data["reason"], design.cannot_vote_for_yourself)
        self.assertEqual(ThumbsUp.objects.filter(entry=entry).count(), 2)

        # skiessi unvote
        self.assertEqual(self.client.login(username="******", password="******"), True)
        entry = Entry.objects.filter(owner=self.superjoe)[0]
        response = self.client.get(reverse(urlname_unvote, args=[entry.id]))
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data["success"], True)
        self.assertEqual(ThumbsUp.objects.filter(entry=entry).count(), 1)

        # make sure his vote count is correct
        response = self.client.get(reverse(urlname_compo, args=[comp.id]))
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data["votes"]["max"], 1)
        self.assertEqual(len(data["votes"]["used"]), 0)
        self.assertEqual(data["votes"]["left"], 1)

        # try unvoting logged out
        self.client.logout()
        response = self.client.get(reverse(urlname_unvote, args=[entry.id]))
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data["success"], False)
        self.assertEqual(data["reason"], design.not_authenticated)

        # close the competition and then look at ajax_compo
        # make sure person with most votes is first
        comp.vote_deadline = now - timedelta(minutes=1)
        comp.save()
        self.assertEqual(comp.isClosed(), True)
        response = self.client.get(reverse(urlname_compo, args=[comp.id]))
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(len(data["entries"]), 3)
        self.assertEqual(data["entries"][0]["owner"]["id"], self.superjoe.id)
        self.assertEqual(data["entries"][0]["vote_count"], 1)
Example #7
0
    def test_ajax_owned(self):
        url = reverse("arena.ajax_owned")
        # should be not authenticated
        self.client.logout()
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data["user"]["is_authenticated"], False)

        # should be no competitions available
        self.assertEqual(self.client.login(username="******", password="******"), True)
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(len(data["compos"]), 0)

        # make a competition
        now = datetime.now()
        comp = Competition()
        comp.title = "test compo 123"
        comp.host = self.skiessi
        comp.theme = "test theme 123"
        comp.preview_theme = False
        comp.rules = "test rules 123"
        comp.preview_rules = True
        comp.start_date = now + timedelta(minutes=1)
        comp.submit_deadline = now + timedelta(minutes=2)
        comp.listening_party_start_date = comp.submit_deadline
        comp.listening_party_end_date = comp.listening_party_start_date
        comp.vote_period_length = 60  # seconds
        comp.chat_room = ChatRoom.objects.create()
        comp.save()

        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(len(data["compos"]), 0)

        # log in and bookmark the competition
        bookmark_url = reverse("arena.ajax_bookmark", args=[comp.id])
        response = self.client.get(bookmark_url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data["success"], True)

        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(len(data["compos"]), 1)
        self.assertEqual(data["compos"][0]["title"], "test compo 123")
        # compo can't have theme yet because it didn't start yet
        self.assertEqual(data["compos"][0].has_key("theme"), False)
        # rules are on preview though
        self.assertEqual(data["compos"][0]["rules"], "test rules 123")

        # unbookmark it
        unbookmark_url = reverse("arena.ajax_unbookmark", args=[comp.id])
        response = self.client.get(unbookmark_url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data["success"], True)

        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(len(data["compos"]), 0)
Example #8
0
    def test_ajax_available(self):
        """
        tests available, owned, bookmark, and unbookmark
        """
        url = reverse('arena.ajax_available')
        # should be no competitions available
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(len(data['compos']), 0)

        # make a competition
        now = datetime.now()
        comp = Competition()
        comp.title = "test compo 123"
        comp.host = self.skiessi
        comp.theme = "test theme 123"
        comp.preview_theme = False
        comp.rules = "test rules 123"
        comp.preview_rules = True
        comp.start_date = now + timedelta(minutes=1)
        comp.submit_deadline = now + timedelta(minutes=2)
        comp.listening_party_start_date = comp.submit_deadline
        comp.listening_party_end_date = comp.listening_party_start_date
        comp.vote_period_length = 60  # seconds
        comp.chat_room = ChatRoom.objects.create()
        comp.save()

        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(len(data['compos']), 1)
        self.assertEqual(data['compos'][0]['title'], 'test compo 123')
        # compo can't have theme yet because it didn't start yet
        self.assertEqual(data['compos'][0].has_key('theme'), False)
        # rules are on preview though
        self.assertEqual(data['compos'][0]['rules'], 'test rules 123')

        # log in and bookmark the competition
        self.assertEqual(
            self.client.login(username="******", password="******"), True)
        bookmark_url = reverse("arena.ajax_bookmark", args=[comp.id])
        response = self.client.get(bookmark_url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['success'], True)

        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(len(data['compos']), 0)

        # unbookmark it
        unbookmark_url = reverse("arena.ajax_unbookmark", args=[comp.id])
        response = self.client.get(unbookmark_url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['success'], True)

        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(len(data['compos']), 1)

        # delete it
        comp.delete()

        # make sure paging works
        howMany = settings.ITEMS_PER_PAGE * 3  # should be 3 pages
        for _ in range(howMany):
            now = datetime.now()
            comp = Competition()
            comp.title = "title"
            comp.host = self.superjoe
            comp.theme = "theme"
            comp.preview_theme = True
            comp.rules = "rules"
            comp.preview_rules = True
            comp.start_date = now - timedelta(minutes=1)
            comp.submit_deadline = now + timedelta(minutes=1)
            comp.listening_party_start_date = comp.submit_deadline
            comp.listening_party_end_date = comp.listening_party_start_date
            comp.vote_period_length = 60  # seconds
            comp.chat_room = ChatRoom.objects.create()
            comp.save()

        # default first page
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(len(data['compos']), settings.ITEMS_PER_PAGE)
        self.assertEqual(data['compos'][0]['theme'], 'theme')
        self.assertEqual(data['compos'][0]['rules'], 'rules')
        self.assertEqual(data['compos'][0]['title'], 'title')
        self.assertEqual(data['page_count'], 3)
        self.assertEqual(data['page_number'], 1)

        # get second page
        response = self.client.get(url, {"page": 2})
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(len(data['compos']), settings.ITEMS_PER_PAGE)
        self.assertEqual(data['page_count'], 3)
        self.assertEqual(data['page_number'], 2)
Example #9
0
    def test_ajax_competition(self):
        """
        this tests:
            ajax_compo 
            ajax_vote
            ajax_unvote
            ajax_submit_entry
        """
        urlname_compo = 'arena.ajax_compo'

        # request a compo that doesn't exist
        response = self.client.get(reverse(urlname_compo, args=[4]))
        self.assertEqual(response.status_code, 404)

        # create a competition that hasn't started yet
        now = datetime.now()
        comp = Competition()
        comp.title = "test compo 123"
        comp.host = self.skiessi
        comp.theme = "test theme 123"
        comp.preview_theme = False
        comp.rules = "test rules 123"
        comp.preview_rules = False
        comp.start_date = now + timedelta(minutes=1)
        comp.submit_deadline = now + timedelta(minutes=2)
        comp.listening_party_start_date = comp.submit_deadline
        comp.listening_party_end_date = comp.listening_party_start_date
        comp.vote_period_length = 60  # seconds
        comp.chat_room = ChatRoom.objects.create()
        comp.save()

        # check that the request works
        response = self.client.get(reverse(urlname_compo, args=[comp.id]))
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['user']['is_authenticated'], False)
        self.assertEqual(data.has_key('votes'), False)
        self.assertEqual(data['compo'].has_key('theme'), False)
        self.assertEqual(data['compo'].has_key('rules'), False)
        self.assertEqual(data['compo']['title'], "test compo 123")
        self.assertEqual(data['party']['buffer_time'],
                         settings.LISTENING_PARTY_BUFFER_TIME)

        # adjust the date so that it is started
        comp.start_date = now - timedelta(minutes=1)
        comp.save()

        # make sure we can see the rules and theme now
        response = self.client.get(reverse(urlname_compo, args=[comp.id]))
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['compo']['theme'], "test theme 123")
        self.assertEqual(data['compo']['rules'], "test rules 123")

        ########## submitting entries ###############
        now = datetime.now()
        url_submit = reverse("arena.ajax_submit_entry")
        # try to submit to a bogus competition
        self.assertEqual(
            self.client.login(username='******', password='******'), True)
        mp3file = open(absolute('fixtures/silence10s-flstudio-notags.mp3'),
                       'rb')
        response = self.client.post(
            url_submit,
            {
                'compo': 90,  # bogus number
                'entry-file-mp3': mp3file,
                #'entry-file-source': sourcefile,
                'entry-title': "test title 123",
                'entry-comments': "test comments 123",
            })
        mp3file.close()
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['success'], False)
        self.assertEqual(data['reason'], design.competition_not_found)
        self.assertEqual(Entry.objects.count(), 0)
        self.assertEqual(Song.objects.count(), 0)

        # try to submit to a not started competition
        comp.start_date = now + timedelta(minutes=1)
        comp.save()
        mp3file = open(absolute('fixtures/silence10s-flstudio-notags.mp3'),
                       'rb')
        sourcefile = open(absolute('fixtures/blank.flp'), 'rb')
        response = self.client.post(
            url_submit, {
                'compo': comp.id,
                'entry-file-mp3': mp3file,
                'entry-file-source': sourcefile,
                'entry-title': "test title 123",
                'entry-comments': "test comments 123",
            })
        mp3file.close()
        sourcefile.close()
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['success'], False)
        self.assertEqual(data['reason'], design.competition_not_started)
        self.assertEqual(Entry.objects.count(), 0)
        self.assertEqual(Song.objects.count(), 0)

        # try to submit after the deadline
        comp.start_date = now - timedelta(minutes=1)
        comp.submit_deadline = now - timedelta(minutes=1)
        comp.save()
        mp3file = open(absolute('fixtures/silence10s-flstudio-notags.mp3'),
                       'rb')
        sourcefile = open(absolute('fixtures/blank.flp'), 'rb')
        response = self.client.post(
            url_submit, {
                'compo': comp.id,
                'entry-file-mp3': mp3file,
                'entry-file-source': sourcefile,
                'entry-title': "test title 123",
                'entry-comments': "test comments 123",
            })
        mp3file.close()
        sourcefile.close()
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['success'], False)
        self.assertEqual(data['reason'], design.past_submission_deadline)
        self.assertEqual(Entry.objects.count(), 0)
        self.assertEqual(Song.objects.count(), 0)

        # try to submit logged out
        comp.start_date = now - timedelta(minutes=1)
        comp.submit_deadline = now + timedelta(minutes=1)
        comp.save()
        self.client.logout()
        mp3file = open(absolute('fixtures/silence10s-flstudio-notags.mp3'),
                       'rb')
        sourcefile = open(absolute('fixtures/blank.flp'), 'rb')
        response = self.client.post(
            url_submit, {
                'compo': comp.id,
                'entry-file-mp3': mp3file,
                'entry-file-source': sourcefile,
                'entry-title': "test title 123",
                'entry-comments': "test comments 123",
            })
        mp3file.close()
        sourcefile.close()
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['success'], False)
        self.assertEqual(data['reason'], design.not_authenticated)
        self.assertEqual(Entry.objects.count(), 0)
        self.assertEqual(Song.objects.count(), 0)

        # try to submit without providing mp3 file
        self.assertEqual(
            self.client.login(username='******', password='******'), True)
        sourcefile = open(absolute('fixtures/blank.flp'), 'rb')
        response = self.client.post(
            url_submit,
            {
                'compo': comp.id,
                #'entry-file-mp3': mp3file,
                'entry-file-source': sourcefile,
                'entry-title': "test title 123",
                'entry-comments': "test comments 123",
            })
        sourcefile.close()
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['success'], False)
        self.assertEqual(data['reason'], design.mp3_required)
        self.assertEqual(Entry.objects.count(), 0)
        self.assertEqual(Song.objects.count(), 0)

        # submit a file that is too big
        # create a temp file that is > settings.FILE_UPLOAD_SIZE_CAP
        handle = tempfile.NamedTemporaryFile(suffix='mp3', delete=False)
        self.dirtyFiles.append(handle.name)
        bytesWritten = 0
        bufferSize = 1024 * 1024
        while bytesWritten <= settings.FILE_UPLOAD_SIZE_CAP:
            handle.write(" " * bufferSize)
            bytesWritten += bufferSize
        handle.close()
        mp3file = open(handle.name, 'rb')
        sourcefile = open(absolute('fixtures/blank.flp'), 'rb')
        response = self.client.post(
            url_submit, {
                'compo': comp.id,
                'entry-file-mp3': mp3file,
                'entry-file-source': sourcefile,
                'entry-title': "test title 123",
                'entry-comments': "test comments 123",
            })
        mp3file.close()
        sourcefile.close()
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['success'], False)
        self.assertEqual(data['reason'], design.mp3_too_big)
        self.assertEqual(Entry.objects.count(), 0)
        self.assertEqual(Song.objects.count(), 0)
        os.remove(handle.name)

        # submit an mp3 file that is too long
        mp3file = open(absolute('fixtures/silence11m-notags.mp3'), 'rb')
        sourcefile = open(absolute('fixtures/blank.flp'), 'rb')
        response = self.client.post(
            url_submit, {
                'compo': comp.id,
                'entry-file-mp3': mp3file,
                'entry-file-source': sourcefile,
                'entry-title': "test title 123",
                'entry-comments': "test comments 123",
            })
        mp3file.close()
        sourcefile.close()
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['success'], False)
        self.assertEqual(data['reason'], design.song_too_long)
        self.assertEqual(Entry.objects.count(), 0)
        self.assertEqual(Song.objects.count(), 0)

        # submit an ogg file instead of mp3
        mp3file = open(absolute('fixtures/silence10s-flstudio-notags.ogg'),
                       'rb')
        sourcefile = open(absolute('fixtures/blank.flp'), 'rb')
        response = self.client.post(
            url_submit, {
                'compo': comp.id,
                'entry-file-mp3': mp3file,
                'entry-file-source': sourcefile,
                'entry-title': "test title 123",
                'entry-comments': "test comments 123",
            })
        mp3file.close()
        sourcefile.close()
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['success'], False)
        self.assertEqual(
            data['reason']
            in (design.sketchy_mp3_file, design.invalid_mp3_file), True)
        self.assertEqual(Entry.objects.count(), 0)
        self.assertEqual(Song.objects.count(), 0)

        # submit a good file, don't provide source or comments
        # flstudio exported, no tags
        self.assertEqual(
            self.client.login(username='******', password='******'), True)
        mp3file = open(absolute('fixtures/silence10s-flstudio-notags.mp3'),
                       'rb')
        response = self.client.post(
            url_submit,
            {
                'compo': comp.id,
                'entry-file-mp3': mp3file,
                #'entry-file-source': sourcefile,
                'entry-title': "superjoe title 123",
                #'entry-comments': "test comments 123",
            })
        mp3file.close()
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['success'], True)
        self.assertEqual(Entry.objects.count(), 1)
        self.assertEqual(Song.objects.count(), 1)
        entry = Entry.objects.filter(owner=self.superjoe)[0]
        # TODO: use storage to ensure path exists
        #self.assertEqual(os.path.exists(os.path.join(settings.MEDIA_ROOT, entry.song.mp3_file)), True)
        #self.assertEqual(os.path.exists(os.path.join(settings.MEDIA_ROOT, entry.song.waveform_img)), True)
        self.assertEqual(entry.song.band.id,
                         self.superjoe.get_profile().solo_band.id)
        self.assertEqual(entry.song.title, 'superjoe title 123')
        version = ProjectVersion.objects.order_by('-pk')[0]
        self.assertNotEqual(version.song.comment_node, None)
        self.assertEqual(version.song.is_open_for_comments, True)

        # submit a good file, provide source and comments. open source.
        # flstudio exported, tags
        self.assertEqual(
            self.client.login(username='******', password='******'),
            True)
        mp3file = open(absolute('fixtures/silence10s-flstudio-tags.mp3'), 'rb')
        sourcefile = open(absolute('fixtures/blank.flp'), 'rb')
        response = self.client.post(
            url_submit, {
                'compo': comp.id,
                'entry-file-mp3': mp3file,
                'entry-file-source': sourcefile,
                'entry-title': "just64helpin title 123",
                'entry-comments': "just64helpin comments 123",
                'entry-open-source': "1",
            })
        mp3file.close()
        sourcefile.close()
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['success'], True)
        self.assertEqual(Entry.objects.count(), 2)
        self.assertEqual(Song.objects.count(), 2)
        entry = Entry.objects.filter(owner=self.just64helpin)[0]
        # TODO: use storage instead of local filesystem
        #self.assertEqual(os.path.exists(os.path.join(settings.MEDIA_ROOT, entry.song.mp3_file)), True)
        #self.assertEqual(os.path.exists(os.path.join(settings.MEDIA_ROOT, entry.song.waveform_img)), True)
        self.assertEqual(entry.song.band.id,
                         self.just64helpin.get_profile().solo_band.id)
        self.assertEqual(entry.song.title, 'just64helpin title 123')
        self.assertEqual(SongCommentNode.objects.count(), 2)
        self.assertEqual(entry.song.comment_node.content,
                         'just64helpin comments 123')
        self.assertEqual(entry.song.is_open_source, True)
        version = ProjectVersion.objects.order_by('-pk')[0]
        self.assertEqual(version.song.comment_node.content,
                         'just64helpin comments 123')
        self.assertEqual(version.song.is_open_for_comments, True)

        # submit a good file, provide source but no comments. not open source.
        # no tags, vbr
        self.assertEqual(
            self.client.login(username='******', password='******'), True)
        mp3file = open(absolute('fixtures/silence10s-notags-vbr.mp3'), 'rb')
        sourcefile = open(absolute('fixtures/blank.flp'), 'rb')
        response = self.client.post(
            url_submit,
            {
                'compo': comp.id,
                'entry-file-mp3': mp3file,
                'entry-file-source': sourcefile,
                'entry-title': "skiessi title 123",
                #'entry-comments': "skiessi comments 123",
                #'entry-open-source': "1",
            })
        mp3file.close()
        sourcefile.close()
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['success'], True)
        self.assertEqual(Entry.objects.count(), 3)
        self.assertEqual(Song.objects.count(), 3)
        entry = Entry.objects.filter(owner=self.skiessi)[0]
        # TODO: use storage instead of local file system
        #self.assertEqual(os.path.exists(os.path.join(settings.MEDIA_ROOT, entry.song.mp3_file)), True)
        #self.assertEqual(os.path.exists(os.path.join(settings.MEDIA_ROOT, entry.song.waveform_img)), True)
        self.assertEqual(entry.song.band.id,
                         self.skiessi.get_profile().solo_band.id)
        self.assertEqual(entry.song.title, 'skiessi title 123')
        self.assertEqual(entry.song.is_open_source, False)
        version = ProjectVersion.objects.order_by('-pk')[0]
        self.assertNotEqual(version.song.comment_node, None)
        self.assertEqual(version.song.is_open_for_comments, True)

        # resubmit
        mp3file = open(absolute('fixtures/silence10s-notags-vbr.mp3'), 'rb')
        sourcefile = open(absolute('fixtures/blank.flp'), 'rb')
        response = self.client.post(
            url_submit, {
                'compo': comp.id,
                'entry-file-mp3': mp3file,
                'entry-file-source': sourcefile,
                'entry-title': "skiessi title 123 v2",
                'entry-comments': "skiessi comments 123 v2",
            })
        mp3file.close()
        sourcefile.close()
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['success'], True)
        self.assertEqual(Entry.objects.count(), 3)
        self.assertEqual(Song.objects.count(), 4)  # new version
        self.assertEqual(SongCommentNode.objects.count(), 4)
        entry = Entry.objects.filter(owner=self.skiessi)[0]
        # TODO: use storage instead of local file system
        #self.assertEqual(os.path.exists(os.path.join(settings.MEDIA_ROOT, entry.song.mp3_file)), True)
        #self.assertEqual(os.path.exists(os.path.join(settings.MEDIA_ROOT, entry.song.waveform_img)), True)
        self.assertEqual(entry.song.band.id,
                         self.skiessi.get_profile().solo_band.id)
        self.assertEqual(entry.song.title, 'skiessi title 123 v2')
        self.assertEqual(entry.song.comment_node.content,
                         'skiessi comments 123 v2')
        version = ProjectVersion.objects.order_by('-pk')[0]
        self.assertEqual(version.song.comment_node.content,
                         'skiessi comments 123 v2')
        self.assertEqual(version.version, 2)
        self.assertEqual(version.song.is_open_for_comments, True)

        # check ajax_compo to make sure it gets the entry list right
        response = self.client.get(reverse(urlname_compo, args=[comp.id]))
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['user']['is_authenticated'], True)
        self.assertEqual(len(data['entries']), 3)
        self.assertEqual(data['entries'][0]['owner']['username'], 'superjoe')
        self.assertEqual(data['entries'][1]['owner']['username'],
                         'just64helpin')
        self.assertEqual(data['entries'][2]['owner']['username'], 'skiessi')

        ############# voting ################
        urlname_vote = 'arena.ajax_vote'
        urlname_unvote = 'arena.ajax_unvote'
        comp.have_listening_party = False
        comp.submit_deadline = now - timedelta(minutes=1)
        comp.vote_deadline = now + timedelta(minutes=1)
        comp.save()

        ############ voting ###############
        # try to vote logged out
        self.client.logout()
        response = self.client.get(reverse(urlname_vote, args=[entry.id]))
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['success'], False)
        self.assertEqual(data['reason'], design.not_authenticated)
        self.assertEqual(ThumbsUp.objects.count(), 0)

        # 2 people vote for superjoe, make sure he has 2 votes
        # skiessi vote for superjoe
        self.assertEqual(
            self.client.login(username='******', password='******'), True)
        entry = Entry.objects.filter(owner=self.superjoe)[0]
        response = self.client.get(reverse(urlname_vote, args=[entry.id]))
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['success'], True)
        self.assertEqual(ThumbsUp.objects.filter(entry=entry).count(), 1)

        # just64helpin vote for superjoe
        self.assertEqual(
            self.client.login(username='******', password='******'),
            True)
        response = self.client.get(reverse(urlname_vote, args=[entry.id]))
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['success'], True)
        self.assertEqual(ThumbsUp.objects.filter(entry=entry).count(), 2)

        # make sure his vote count is decremented
        response = self.client.get(reverse(urlname_compo, args=[comp.id]))
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['votes']['max'], 1)
        self.assertEqual(len(data['votes']['used']), 1)
        self.assertEqual(data['votes']['used'][0]['entry'], entry.id)
        self.assertEqual(data['votes']['left'], 0)

        # vote for yourself (invalid)
        self.assertEqual(
            self.client.login(username='******', password='******'), True)
        response = self.client.get(reverse(urlname_vote, args=[entry.id]))
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['success'], False)
        self.assertEqual(data['reason'], design.cannot_vote_for_yourself)
        self.assertEqual(ThumbsUp.objects.filter(entry=entry).count(), 2)

        # skiessi unvote
        self.assertEqual(
            self.client.login(username='******', password='******'), True)
        entry = Entry.objects.filter(owner=self.superjoe)[0]
        response = self.client.get(reverse(urlname_unvote, args=[entry.id]))
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['success'], True)
        self.assertEqual(ThumbsUp.objects.filter(entry=entry).count(), 1)

        # make sure his vote count is correct
        response = self.client.get(reverse(urlname_compo, args=[comp.id]))
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['votes']['max'], 1)
        self.assertEqual(len(data['votes']['used']), 0)
        self.assertEqual(data['votes']['left'], 1)

        # try unvoting logged out
        self.client.logout()
        response = self.client.get(reverse(urlname_unvote, args=[entry.id]))
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['success'], False)
        self.assertEqual(data['reason'], design.not_authenticated)

        # close the competition and then look at ajax_compo
        # make sure person with most votes is first
        comp.vote_deadline = now - timedelta(minutes=1)
        comp.save()
        self.assertEqual(comp.isClosed(), True)
        response = self.client.get(reverse(urlname_compo, args=[comp.id]))
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(len(data['entries']), 3)
        self.assertEqual(data['entries'][0]['owner']['id'], self.superjoe.id)
        self.assertEqual(data['entries'][0]['vote_count'], 1)
Example #10
0
    def test_ajax_owned(self):
        url = reverse('arena.ajax_owned')
        # should be not authenticated
        self.client.logout()
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['user']['is_authenticated'], False)

        # should be no competitions available
        self.assertEqual(
            self.client.login(username="******", password="******"), True)
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(len(data['compos']), 0)

        # make a competition
        now = datetime.now()
        comp = Competition()
        comp.title = "test compo 123"
        comp.host = self.skiessi
        comp.theme = "test theme 123"
        comp.preview_theme = False
        comp.rules = "test rules 123"
        comp.preview_rules = True
        comp.start_date = now + timedelta(minutes=1)
        comp.submit_deadline = now + timedelta(minutes=2)
        comp.listening_party_start_date = comp.submit_deadline
        comp.listening_party_end_date = comp.listening_party_start_date
        comp.vote_period_length = 60  # seconds
        comp.chat_room = ChatRoom.objects.create()
        comp.save()

        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(len(data['compos']), 0)

        # log in and bookmark the competition
        bookmark_url = reverse("arena.ajax_bookmark", args=[comp.id])
        response = self.client.get(bookmark_url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['success'], True)

        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(len(data['compos']), 1)
        self.assertEqual(data['compos'][0]['title'], 'test compo 123')
        # compo can't have theme yet because it didn't start yet
        self.assertEqual(data['compos'][0].has_key('theme'), False)
        # rules are on preview though
        self.assertEqual(data['compos'][0]['rules'], 'test rules 123')

        # unbookmark it
        unbookmark_url = reverse("arena.ajax_unbookmark", args=[comp.id])
        response = self.client.get(unbookmark_url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['success'], True)

        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(len(data['compos']), 0)