def test_get_free_token_fail_userpass(self, mock_uuid, mock_random):
     mock_uuid.side_effect = fakes.FAKE_UUID
     mock_random.side_effect = fakes.FAKE_RANDOM
     responses.add(responses.POST,
                   config.TOKEN_URL,
                   body=json.dumps({'token': 'abcdef'}),
                   status=200)
     responses.add(responses.POST,
                   config.AFL_LOGIN_URL,
                   body=self.MIS_UUID_JSON,
                   status=200)
     escaped_spc_url = re.escape(config.SPORTSPASS_URL).replace(
         '\\{', '{').replace('\\}', '}')
     spc_url = re.compile(escaped_spc_url.format('.*'))
     responses.add(responses.GET,
                   spc_url,
                   body=self.SPC_RESP_HTML,
                   status=200)
     responses.add(responses.GET,
                   config.MYID_AUTHORIZATION_URL,
                   body=self.MYID_AUTH_RESP_HTML,
                   status=200)
     responses.add(responses.POST,
                   config.MYID_RESUME_AUTHORIZATION_URL.format('ABCDE'),
                   body=self.MYID_AUTH_RESP_HTML,
                   status=200)
     auth = telstra_auth.TelstraAuth('foo', 'wrongpassword')
     self.assertRaises(telstra_auth.TelstraAuthException,
                       auth.get_free_token)
 def test_get_free_token_fail_userpass(self, mock_uuid, mock_random):
     mock_uuid.side_effect = fakes.FAKE_UUID
     mock_random.side_effect = fakes.FAKE_RANDOM
     responses.add(responses.POST,
                   config.YINZCAM_AUTH_URL,
                   body=self.YINZCAM_AUTH_RESP_XML,
                   status=200)
     responses.add(responses.GET,
                   config.YINZCAM_AUTH_URL2,
                   body=self.YINZCAM_AUTH2_RESP_JSON,
                   status=200)
     responses.add(responses.GET,
                   json.loads(self.YINZCAM_AUTH2_RESP_JSON).get('Url'),
                   body=self.SPC_RESP_HTML,
                   status=200)
     responses.add(responses.GET,
                   config.MYID_AUTHORIZATION_URL,
                   body=self.MYID_AUTH_RESP_HTML,
                   status=200)
     responses.add(responses.POST,
                   config.MYID_RESUME_AUTHORIZATION_URL.format('ABCDE'),
                   body=self.MYID_AUTH_RESP_HTML,
                   status=200)
     auth = telstra_auth.TelstraAuth('foo', 'wrongpassword')
     self.assertRaises(telstra_auth.TelstraAuthException,
                       auth.get_free_token)
 def test_get_mobile_token(self):
     responses.add(responses.POST,
                   config.TOKEN_URL,
                   body=json.dumps({'token': 'abcdef'}),
                   status=200)
     responses.add(responses.POST,
                   config.AFL_LOGIN_URL,
                   body=self.MIS_UUID_JSON,
                   status=200)
     responses.add(responses.GET,
                   config.MOBILE_ID_URL,
                   headers={'Set-Cookie': fakes.FAKE_MOBILE_COOKIE},
                   status=200)
     responses.add(responses.POST,
                   config.MOBILE_OAUTH_URL,
                   body=self.AFL_TOKEN_JSON,
                   status=200)
     responses.add(responses.GET,
                   config.OFFERS_URL,
                   body=self.OFFERS_RESP_JSON,
                   status=200)
     responses.add(responses.POST,
                   config.MEDIA_ORDER_URL,
                   body=self.ORDER_RESP_JSON,
                   status=201)
     auth = telstra_auth.TelstraAuth()
     observed = auth.get_mobile_token()
     self.assertEqual(fakes.MIS_UUID, observed)
    def test_get_free_token(self, mock_uuid, mock_random):
        mock_uuid.side_effect = fakes.FAKE_UUID
        mock_random.side_effect = fakes.FAKE_RANDOM
        responses.add(responses.POST,
                      config.TOKEN_URL,
                      body=json.dumps({'token': 'abcdef'}),
                      status=200)
        responses.add(responses.POST,
                      config.AFL_LOGIN_URL,
                      body=self.MIS_UUID_JSON,
                      status=200)
        escaped_spc_url = re.escape(config.SPORTSPASS_URL).replace(
            '\\{', '{').replace('\\}', '}')
        spc_url = re.compile(escaped_spc_url.format('.*'))
        responses.add(responses.GET,
                      spc_url,
                      body=self.SPC_RESP_HTML,
                      status=200)
        responses.add(responses.GET,
                      config.MYID_AUTHORIZATION_URL,
                      body=self.MYID_AUTH_RESP_HTML,
                      status=200)
        responses.add(responses.POST,
                      config.MYID_RESUME_AUTHORIZATION_URL.format('ABCDE'),
                      body=self.MYID_RESUME_AUTH_RESP_HTML,
                      status=200)
        for url in config.SSO_SESSION_HANDLER_URLS:
            responses.add(responses.POST,
                          url,
                          json={'status': 'success'},
                          status=200)

        responses.add(responses.GET,
                      '{0}{1}'.format(
                          config.MYID_RESUME_AUTHORIZATION_URL.format('ABCDE'),
                          '?ctfr-proceed=true'),
                      headers={
                          'Set-Cookie': fakes.FAKE_BPSESSION_COOKIE,
                          'Location': fakes.MYID_RESUME_AUTH_REDIRECT_URL
                      },
                      status=302)
        responses.add(responses.GET,
                      fakes.MYID_RESUME_AUTH_REDIRECT_URL,
                      status=200)
        responses.add(responses.POST,
                      config.MYID_TOKEN_URL,
                      body=self.MYID_TOKEN_RESP_JSON,
                      status=200)
        responses.add(responses.GET,
                      config.OFFERS_URL,
                      body=self.OFFERS_RESP_JSON,
                      status=200)
        responses.add(responses.POST,
                      config.MEDIA_ORDER_URL,
                      body=self.ORDER_RESP_JSON,
                      status=201)
        auth = telstra_auth.TelstraAuth('foo', 'bar')
        observed = auth.get_free_token()
        self.assertEqual(fakes.MIS_UUID, observed)
    def test_get_free_token_fail_no_eligible(self, mock_uuid, mock_random):
        mock_uuid.side_effect = fakes.FAKE_UUID
        mock_random.side_effect = fakes.FAKE_RANDOM
        spc_url = json.loads(self.YINZCAM_AUTH2_RESP_JSON).get('Url')
        responses.add(responses.POST,
                      config.YINZCAM_AUTH_URL,
                      body=self.YINZCAM_AUTH_RESP_JSON,
                      status=200)
        responses.add(responses.GET,
                      config.YINZCAM_AUTH_URL2,
                      body=self.YINZCAM_AUTH2_RESP_JSON,
                      status=200)
        responses.add(responses.GET,
                      spc_url,
                      body=self.SPC_RESP_HTML,
                      status=200)
        responses.add(responses.GET,
                      config.MYID_AUTHORIZATION_URL,
                      body=self.MYID_AUTH_RESP_HTML,
                      status=200)
        responses.add(responses.POST,
                      config.MYID_RESUME_AUTHORIZATION_URL.format('ABCDE'),
                      body=self.MYID_RESUME_AUTH_RESP_HTML,
                      status=200)
        for url in config.SSO_SESSION_HANDLER_URLS:
            responses.add(responses.POST,
                          url,
                          json={'status': 'success'},
                          status=200)

        responses.add(responses.GET,
                      '{0}{1}'.format(
                          config.MYID_RESUME_AUTHORIZATION_URL.format('ABCDE'),
                          '?ctfr-proceed=true'),
                      headers={
                          'Set-Cookie': fakes.FAKE_BPSESSION_COOKIE,
                          'Location': fakes.MYID_RESUME_AUTH_REDIRECT_URL
                      },
                      status=302)
        responses.add(responses.GET,
                      fakes.MYID_RESUME_AUTH_REDIRECT_URL,
                      status=200)
        responses.add(responses.POST,
                      config.MYID_TOKEN_URL,
                      body=self.MYID_TOKEN_RESP_JSON,
                      status=200)
        responses.add(responses.GET,
                      config.OFFERS_URL,
                      json={'userMessage': 'No eligible services'},
                      status=404)
        auth = telstra_auth.TelstraAuth('foo', 'bar')
        self.assertRaises(telstra_auth.TelstraAuthException,
                          auth.get_free_token)
def get_user_ticket():
    """
    send user login info and retrieve ticket for session
    """
    stored_ticket = cache.get('NRLTICKET')
    if stored_ticket != '':
        utils.log('Using ticket: {0}******'.format(stored_ticket[:-6]))
        return stored_ticket
    sub_type = int(addon.getSetting('SUBSCRIPTION_TYPE'))

    if sub_type == 1:
        auth = telstra_auth.TelstraAuth(telstra_username, telstra_password)
        ticket = auth.get_free_token()
    elif sub_type == 2:  # mobile activated subscription
        auth = telstra_auth.TelstraAuth()
        ticket = auth.get_mobile_token()
    else:
        auth = telstra_auth.TelstraAuth(nrl_username, nrl_password)
        ticket = auth.get_paid_token()
    cache.set('NRLTICKET', ticket)
    return ticket
Exemplo n.º 7
0
def get_user_ticket():
    """
    send user login info and retrieve ticket for session
    """
    stored_ticket = cache.get('NETBALLTICKET')
    if stored_ticket != '':
        utils.log('Using ticket: {0}******'.format(stored_ticket[:-6]))
        return stored_ticket
    else:
        auth = telstra_auth.TelstraAuth(username, password)
        ticket = auth.get_free_token()
    cache.set('NETBALLTICKET', ticket)
    return ticket
    def test_get_paid_token(self):
        def auth_callback(request):
            headers = {}
            if 'x-xsrf-token' in request.headers:
                location = fakes.AUTH_REDIRECT_CODE_URL
            else:
                headers.update({'Set-Cookie': fakes.FAKE_XSRF_COOKIE})
                location = fakes.AUTH_REDIRECT_URL
            headers.update({'Location': location})
            return (302, headers, '')

        def login_callback(request):
            payload = json.loads(request.body)
            if (payload.get('emailAddress') == 'foo'
                    and payload.get('password') == 'bar'):
                body = json.dumps({'success': True})
            else:
                body = json.dumps({
                    'success': False,
                    'error': 'Invalid email address or password'
                })
            return (200, {}, body)

        responses.add_callback(responses.GET,
                               config.NRL_AUTH,
                               callback=auth_callback)
        responses.add(responses.GET,
                      re.compile(config.NRL_AUTH_ACCEPT),
                      body='',
                      headers={'location': 'http://foo.bar/?code=abc'})
        responses.add_callback(responses.POST,
                               config.NRL_LOGIN,
                               callback=login_callback)
        responses.add(responses.POST,
                      config.NRL_TOKEN,
                      body=self.NRL_TOKEN_JSON,
                      status=200)
        responses.add(responses.POST,
                      config.YINZCAM_AUTH_URL.format(
                          json.loads(
                              self.NRL_TOKEN_JSON).get('refresh_token')),
                      body=self.YINZCAM_AUTH_RESP_JSON,
                      status=200)
        responses.add(responses.GET,
                      config.STATUS_URL,
                      body=self.STATUS_RESP_JSON,
                      status=200)
        auth = telstra_auth.TelstraAuth('foo', 'bar')
        observed = auth.get_paid_token()
        self.assertEqual('ticket123', observed)
 def test_get_mobile_token_fail_no_mobile_data(self):
     responses.add(responses.POST,
                   config.YINZCAM_AUTH_URL,
                   body=self.YINZCAM_AUTH_RESP_XML,
                   status=200)
     responses.add(responses.GET,
                   config.YINZCAM_AUTH_URL2,
                   body=self.YINZCAM_AUTH2_RESP_JSON,
                   status=200)
     responses.add(responses.GET,
                   config.MOBILE_ID_URL,
                   headers={'Set-Cookie': fakes.FAKE_MOBILE_COOKIE_NO_DATA},
                   status=204)
     auth = telstra_auth.TelstraAuth()
     self.assertRaises(telstra_auth.TelstraAuthException,
                       auth.get_mobile_token)
 def test_get_mobile_token_fail_no_mobile_data(self):
     responses.add(responses.POST,
                   config.TOKEN_URL,
                   body=json.dumps({'token': 'abcdef'}),
                   status=200)
     responses.add(responses.POST,
                   config.AFL_LOGIN_URL,
                   body=self.MIS_UUID_JSON,
                   status=200)
     responses.add(responses.GET,
                   config.MOBILE_ID_URL,
                   headers={'Set-Cookie': fakes.FAKE_MOBILE_COOKIE_NO_DATA},
                   status=204)
     auth = telstra_auth.TelstraAuth()
     self.assertRaises(telstra_auth.TelstraAuthException,
                       auth.get_mobile_token)
 def test_get_mobile_token_fail_activation(self):
     responses.add(responses.POST,
                   config.YINZCAM_AUTH_URL,
                   body=self.YINZCAM_AUTH_RESP_XML,
                   status=200)
     responses.add(responses.GET,
                   config.YINZCAM_AUTH_URL2,
                   body=self.YINZCAM_AUTH2_RESP_JSON,
                   status=200)
     responses.add(responses.GET,
                   config.MOBILE_ID_URL,
                   headers={'Set-Cookie': fakes.FAKE_MOBILE_COOKIE},
                   status=200)
     responses.add(responses.POST,
                   config.MOBILE_OAUTH_URL,
                   body=self.NRL_TOKEN_JSON,
                   status=200)
     responses.add(responses.GET,
                   config.OFFERS_URL,
                   body=self.OFFERS_RESP_JSON,
                   status=200)
     responses.add(responses.POST,
                   config.MEDIA_ORDER_URL,
                   body=self.ORDER_RESP_JSON,
                   status=200)
     responses.add(responses.GET,
                   config.YINZ_CALLBACK_URL.format(
                       json.loads(
                           self.YINZCAM_AUTH2_RESP_JSON).get('TpUid')),
                   headers={'Location': 'foo://bar'},
                   status=200)
     responses.add(responses.GET,
                   config.STATUS_URL,
                   body=self.STATUS_FAIL_RESP_JSON,
                   status=200)
     auth = telstra_auth.TelstraAuth()
     self.assertRaises(telstra_auth.TelstraAuthException,
                       auth.get_mobile_token)
 def test_get_mobile_token_fail_no_eligible(self):
     responses.add(responses.POST,
                   config.YINZCAM_AUTH_URL,
                   body=self.YINZCAM_AUTH_RESP_XML,
                   status=200)
     responses.add(responses.GET,
                   config.YINZCAM_AUTH_URL2,
                   body=self.YINZCAM_AUTH2_RESP_JSON,
                   status=200)
     responses.add(responses.GET,
                   config.MOBILE_ID_URL,
                   headers={'Set-Cookie': fakes.FAKE_MOBILE_COOKIE},
                   status=200)
     responses.add(responses.POST,
                   config.MOBILE_OAUTH_URL,
                   body=self.NRL_TOKEN_JSON,
                   status=200)
     responses.add(responses.GET,
                   config.OFFERS_URL,
                   json={'userMessage': 'No eligible services'},
                   status=404)
     auth = telstra_auth.TelstraAuth()
     self.assertRaises(telstra_auth.TelstraAuthException,
                       auth.get_mobile_token)
 def test_get_mobile_token_fail_no_eligible(self):
     responses.add(responses.POST,
                   config.TOKEN_URL,
                   body=json.dumps({'token': 'abcdef'}),
                   status=200)
     responses.add(responses.POST,
                   config.AFL_LOGIN_URL,
                   body=self.MIS_UUID_JSON,
                   status=200)
     responses.add(responses.GET,
                   config.MOBILE_ID_URL,
                   headers={'Set-Cookie': fakes.FAKE_MOBILE_COOKIE},
                   status=200)
     responses.add(responses.POST,
                   config.MOBILE_OAUTH_URL,
                   body=self.AFL_TOKEN_JSON,
                   status=200)
     responses.add(responses.GET,
                   config.OFFERS_URL,
                   json={'userMessage': 'No eligible services'},
                   status=404)
     auth = telstra_auth.TelstraAuth()
     self.assertRaises(telstra_auth.TelstraAuthException,
                       auth.get_mobile_token)
    def test_get_free_token_fail_not_activated(self, mock_uuid, mock_random):
        mock_uuid.side_effect = fakes.FAKE_UUID
        mock_random.side_effect = fakes.FAKE_RANDOM
        responses.add(responses.POST,
                      config.YINZCAM_AUTH_URL,
                      body=self.YINZCAM_AUTH_RESP_XML,
                      status=200)
        responses.add(responses.GET,
                      config.YINZCAM_AUTH_URL2,
                      body=self.YINZCAM_AUTH2_RESP_JSON,
                      status=200)
        responses.add(responses.GET,
                      json.loads(self.YINZCAM_AUTH2_RESP_JSON).get('Url'),
                      body=self.SPC_RESP_HTML,
                      status=200)
        responses.add(responses.GET,
                      config.MYID_AUTHORIZATION_URL,
                      body=self.MYID_AUTH_RESP_HTML,
                      status=200)
        responses.add(responses.POST,
                      config.MYID_RESUME_AUTHORIZATION_URL.format('ABCDE'),
                      body=self.MYID_RESUME_AUTH_RESP_HTML,
                      status=200)
        for url in config.SSO_SESSION_HANDLER_URLS:
            responses.add(responses.POST,
                          url,
                          json={'status': 'success'},
                          status=200)

        responses.add(responses.GET,
                      '{0}{1}'.format(
                          config.MYID_RESUME_AUTHORIZATION_URL.format('ABCDE'),
                          '?ctfr-proceed=true'),
                      headers={
                          'Set-Cookie': fakes.FAKE_BPSESSION_COOKIE,
                          'Location': fakes.MYID_RESUME_AUTH_REDIRECT_URL
                      },
                      status=302)
        responses.add(responses.GET,
                      fakes.MYID_RESUME_AUTH_REDIRECT_URL,
                      status=200)
        responses.add(responses.POST,
                      config.MYID_TOKEN_URL,
                      body=self.MYID_TOKEN_RESP_JSON,
                      status=200)
        responses.add(responses.GET,
                      config.OFFERS_URL,
                      body=self.OFFERS_RESP_JSON,
                      status=200)
        responses.add(responses.POST,
                      config.MEDIA_ORDER_URL,
                      body=self.ORDER_RESP_JSON,
                      status=200)
        responses.add(responses.GET,
                      config.YINZ_CALLBACK_URL.format(
                          json.loads(
                              self.YINZCAM_AUTH2_RESP_JSON).get('TpUid')),
                      headers={'Location': 'foo://bar'},
                      status=200)
        responses.add(responses.GET,
                      config.STATUS_URL,
                      body=self.STATUS_FAIL_RESP_JSON,
                      status=200)
        auth = telstra_auth.TelstraAuth('foo', 'bar')
        self.assertRaises(telstra_auth.TelstraAuthException,
                          auth.get_free_token)
    def test_get_free_token(self, mock_uuid, mock_random):
        mock_uuid.side_effect = fakes.FAKE_UUID
        mock_random.side_effect = fakes.FAKE_RANDOM

        spc_url = json.loads(self.YINZCAM_AUTH2_RESP_JSON).get('Url')
        tpuid = json.loads(self.YINZCAM_AUTH2_RESP_JSON).get('TpUid')
        responses.add(responses.POST,
                      config.YINZCAM_AUTH_URL,
                      body=self.YINZCAM_AUTH_RESP_JSON,
                      status=200)
        responses.add(responses.GET,
                      config.YINZCAM_AUTH_URL2,
                      body=self.YINZCAM_AUTH2_RESP_JSON,
                      status=200)
        responses.add(responses.GET,
                      spc_url,
                      body=self.SPC_RESP_HTML,
                      status=200)
        responses.add(responses.GET,
                      config.MYID_AUTHORIZATION_URL,
                      body=self.MYID_AUTH_RESP_HTML,
                      status=200)
        responses.add(responses.POST,
                      config.MYID_RESUME_AUTHORIZATION_URL.format('ABCDE'),
                      body=self.MYID_RESUME_AUTH_RESP_HTML,
                      status=200)
        for url in config.SSO_SESSION_HANDLER_URLS:
            responses.add(responses.POST,
                          url,
                          json={'status': 'success'},
                          status=200)

        responses.add(responses.GET,
                      '{0}{1}'.format(
                          config.MYID_RESUME_AUTHORIZATION_URL.format('ABCDE'),
                          '?ctfr-proceed=true'),
                      headers={
                          'Set-Cookie': fakes.FAKE_BPSESSION_COOKIE,
                          'Location': fakes.MYID_RESUME_AUTH_REDIRECT_URL
                      },
                      status=302)
        responses.add(responses.GET,
                      fakes.MYID_RESUME_AUTH_REDIRECT_URL,
                      status=200)
        responses.add(responses.POST,
                      config.MYID_TOKEN_URL,
                      body=self.MYID_TOKEN_RESP_JSON,
                      status=200)
        responses.add(responses.GET,
                      config.OFFERS_URL,
                      body=self.OFFERS_RESP_JSON,
                      status=200)
        responses.add(responses.POST,
                      config.MEDIA_ORDER_URL,
                      body=self.ORDER_RESP_JSON,
                      status=200)
        responses.add(responses.GET,
                      config.YINZ_CALLBACK_URL.format(tpuid),
                      status=200)
        responses.add(responses.GET,
                      config.STATUS_URL,
                      body=self.STATUS_RESP_JSON,
                      status=200)
        auth = telstra_auth.TelstraAuth('foo', 'bar')
        observed = auth.get_free_token()
        self.assertEqual('ticket123', observed)
Exemplo n.º 16
0
def get_user_token():
    """Send user login info and retrieve token for session"""
    # in-app purchase/manual
    subscription_type = get_sub_type()
    if subscription_type == 2:
        iap_token = addon.getSetting('IAP_TOKEN').lower()
        try:
            int(iap_token, 16)
        except ValueError:
            raise AussieAddonsException(
                'mis-uuid token must be 32 characters in length, and only '
                'contain numbers 0-9 and letters a-f')
        if len(iap_token) != 32:
            raise AussieAddonsException(
                'mis-uuid token must be 32 characters in length, and only '
                'contain numbers 0-9 and letters a-f')
        token = 'mis-uuid-{0}'.format(iap_token)
        utils.log('Using manual token: {0}******'.format(token[:-6]))
        return token

    stored_token = cache.get('AFLTOKEN')
    if stored_token:
        utils.log('Using token: {0}******'.format(stored_token[:-6]))
        return stored_token

    if addon.getSetting('LIVE_SUBSCRIPTION') == 'true':
        username = addon.getSetting('LIVE_USERNAME')
        password = addon.getSetting('LIVE_PASSWORD')

        if subscription_type == 1:  # free subscription
            auth = telstra_auth.TelstraAuth(username, password)
            token = auth.get_free_token()
        elif subscription_type == 3:  # mobile activated subscription
            auth = telstra_auth.TelstraAuth()
            token = auth.get_mobile_token()
        else:  # paid afl.com.au
            login_data = {'userIdentifier': addon.getSetting('LIVE_USERNAME'),
                          'authToken': addon.getSetting('LIVE_PASSWORD'),
                          'userIdentifierType': 'EMAIL'}
            login_json = fetch_session_id(config.LOGIN_URL, login_data)
            data = json.loads(login_json)
            if data.get('responseCode') != 0:
                raise AussieAddonsException('Invalid Telstra ID login/'
                                            'password for paid afl.com.au '
                                            '/ linked subscription.')
            session_id = data['data'].get('artifactValue')

            try:
                sess.headers.update({'Authorization': None})
                encoded_session_id = quote(session_id)
                session_url = config.SESSION_URL.format(encoded_session_id)
                res = sess.get(session_url)
                data = json.loads(res.text)
                token = data.get('uuid')

            except requests.exceptions.HTTPError as e:
                utils.log(e.response.text)
                raise e
        cache.set('AFLTOKEN', token)
        utils.log('Using token: {0}******'.format(token[:-6]))
        return token
    else:
        raise AussieAddonsException('AFL Live Pass subscription is required '
                                    'for this content. Please open the '
                                    'add-on settings to enable and configure.')