user2471177
user2471177

Reputation: 21

About interacting and testing with django tastypie

I read many tutorial about django testing but I don't know how can I test for function insde resource , for example this User resource with signin function and obj_create. I really appreciate any helps because I can not figure out how to test them. k thanks.

class UserResource(ModelResource):

school = fields.ToOneField('frittie.app.api.api.LocationResource', 'user')
class Meta:
    queryset = UserProfile.objects.all()
    resource_name = 'User'
    allowed_methods = ['get','post']
    serializer = Serializer(formats=['json', 'plist'])

    authorization= Authorization()
    #models.signals.post_save.connect(create_api_key, sender=User)
    #fields = ['username', 'email']
    def obj_create(self, bundle, request=None, **kwargs):
        if not request.method == "POST":
            raise BadRequest('Object not found or not allowed to create a new one.')
        username, email, password = bundle.data['username'], bundle.data['password'], bundle.data['password'], 
        try:
            bundle.obj = User.objects.create_user(username, email, password)
        except IntegrityError:
            raise BadRequest('That username already exists')
        return bundle

    def signin(self, request, **kwargs):
        self.method_check(request, allowed=['post'])
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)

        if user is not None:
            if user.is_active:
                login(request, user)
                return self.create_response(request, {'success': True})
            else:
                # Return a 'disabled account' error message
                return self.create_response(request, {'success': False})
        else:
            # Return an 'invalid login' error message.
            return self.create_response(request, {'success': False})

Upvotes: 0

Views: 965

Answers (1)

ge7600
ge7600

Reputation: 399

Tastypie has a descent testing documentation - ResourceTestCase API Reference

Upvotes: 1

Related Questions