[Flickr] handle unavailable API
[mygpo.git] / mygpo / test.py
blob322296ded6f0e5a3fe6bd7bb52b31b5f9da74d4c
1 import os.path
2 import base64
4 from django.core.urlresolvers import resolve
5 from django.contrib.auth.models import AnonymousUser
6 from django.test.client import RequestFactory
7 from django.contrib.auth import get_user_model
9 from mygpo.utils import random_token
12 def create_auth_string(username, password):
13 pwdstr = '{0}:{1}'.format(username, password).rstrip()
14 credentials = base64.b64encode(pwdstr.encode('utf-8'))
15 auth_string = 'Basic ' + credentials.decode('ascii')
16 return auth_string
19 def anon_request(url):
20 """ Emulates an anonymous request, returns the response
22 """
23 factory = RequestFactory()
24 request = factory.get(url)
25 request.user = AnonymousUser()
26 view = resolve(url)
28 response = view.func(request, *view.args, **view.kwargs)
29 return response
32 def create_user():
33 """ Create a user with random data """
34 User = get_user_model()
35 password = random_token(10)
36 username = random_token(8)
37 user = User(username=username, email=username + '@example.com')
38 user.set_password(password)
39 user.is_active = True
40 user.save()
41 return user, password