Example #1
0
    def test_list_IPTSs_GPSANS(self):
        '''
        '''

        instrument = 'CG2'

        user = get_user_model().objects.create_user(env("TEST_USERNAME"),
                                                    '*****@*****.**',
                                                    env("TEST_PASSWORD"))
        profile = UserProfile(
            **{
                'user': user,
                'home_institution': 'Test',
                'facility': Facility.objects.get(pk=1),
                'instrument': Instrument.objects.get(icat_name=instrument),
                'ipts': 'IPTS-19748',
                'experiment': 'exp240',
            })
        profile.save()

        # Setup request and view.
        request = self.factory.get(
            reverse('catalog:list_iptss', kwargs={'instrument': instrument}))
        request.user = user
        request.session = {'password': env("TEST_PASSWORD")}

        view = IPTSs.as_view()
        response = view(request, instrument=instrument)

        # Check.
        self.assertEqual(response.status_code, 200)
        self.assertTrue(response.context_data['iptss'])
Example #2
0
    def test_create_profile(self):
        '''
        To run this test:
        ./manage.py test server.apps.users.test.TestLogin.test_create_profile
        '''
        login = self.client.login(username=env("TEST_USERNAME"),
                                  password=env("TEST_PASSWORD"))
        self.assertTrue(login)

        user = get_user_model().objects.get(username=env("TEST_USERNAME"))
        self.assertEqual(user.username, env("TEST_USERNAME"))

        response = self.client.post(reverse('users:profile_create'), {
            'user': user,
            'home_institution': 'Test',
            'facility': 1,
            'instrument': 2,
            'ipts': 'IPTS-828',
            'experiment': 'exp100'
        },
                                    follow=True)

        user_profile = UserProfile.objects.get(
            user__username=env("TEST_USERNAME"))

        self.assertEqual(user_profile.user.username, env("TEST_USERNAME"))

        # response.redirect_chain = [('/', 302), ('/users/profile', 302)]
        self.assertTrue(
            reverse('users:profile_view') in
            [red[0] for red in response.redirect_chain])
        self.assertEqual(response.status_code, 200)
Example #3
0
    def test_login_real_credentials_return_code(self):

        response = self.client.get(reverse('users:login'))
        self.assertEqual(200, response.status_code, "Expected ok status.")

        response = self.client.post(
            '/users/login',
            dict(
                username=env("TEST_USERNAME"),
                password=env("TEST_PASSWORD"),
            ),
        )

        self.assertEqual(302, response.status_code,
                         "Expected redirect status to profile create.")

        # "Expected redirect status to profile create.
        response = self.client.get(
            reverse('users:profile_view'),
            follow=True,
        )
        self.assertEqual(200, response.status_code, "Expected ok status.")
Example #4
0
    def form_valid(self, form):
        """
        If the form is valid this is called!
        Checks what kind of button was pressed
        """

        if 'save' in self.request.POST:
            messages.success(self.request, "Reduction script saved.")
            self.success_url = reverse_lazy('reduction:detail',
                                            args=[self.kwargs['pk']])
        elif 'generate' in self.request.POST:
            messages.success(self.request,
                             "Reduction script re-generated from scratch.")
            self.success_url = reverse_lazy('reduction:script',
                                            args=[self.kwargs['pk']])
        else:
            try:
                server_name = env("JOB_SERVER_NAME")
                server = get_object_or_404(Server, title=server_name)

                job = self._create_job(form, server)
                form.instance.job = job

                password_encrypted = self.request.session["password"]
                password = signing.loads(password_encrypted)

                task = self._get_remote_task(form)
                logger.debug("Executing remotely: {}".format(task))
                transaction.on_commit(lambda: task.delay(
                    job.pk,
                    password=password,
                    log_policy=self.log_policy,
                    store_results=self.store_results,
                    remote=True,
                ))
                messages.success(
                    self.request,
                    "Reduction submitted to the cluster. See status: \
                    <a href='%s'> here </a>" %
                    reverse_lazy("results:job_log_live", args=[job.pk]))
            except Exception as e:
                logger.exception(e)
                messages.error(
                    self.request, "Reduction not submitted to the cluster. \
                    An exception occurred: {0} :: \
                    {1}".format(type(e).__name__, str(e)))
        return super().form_valid(form)
Example #5
0
 def test_login_function_real_credentials(self):
     login = self.client.login(
         username=env("TEST_USERNAME"),
         password=env("TEST_PASSWORD"),
     )
     self.assertTrue(login)