0

我阅读了很多关于 django 测试的教程,但我不知道如何测试函数 insde 资源,例如这个带有登录函数和 obj_create 的用户资源。我真的很感激任何帮助,因为我不知道如何测试它们。谢谢。

类用户资源(模型资源):

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})
4

1 回答 1

1

Tastypie 有一个血统测试文档 - ResourceTestCase API 参考

于 2013-07-02T04:34:59.813 回答