我正在编写一个类似Twitter的基于Django的RESTful应用程序,该应用程序支持基本功能,例如登录,注册,创建/删除/阅读推文,关注/取消关注。我想为我的应用程序编写测试以确保其正常运行。
我尝试使用APIRequestFactory()但徒劳。
我的用户类别
class User(models.Model):
user_id = models.AutoField(primary_key=True)
email = models.EmailField(max_length=80, default="email")
username = models.CharField(max_length=280, unique=True)
password = models.CharField(max_length=20)
isactive = models.BooleanField(default=False)
def __str__(self):
return str(self.username)
我的注册自定义视图:
@csrf_exempt
def RegistrationView(request):
if request.method == 'POST':
body_unicode = request.body.decode('utf-8')
body = json.loads(body_unicode)
email = body['email']
username = body['username']
temp_username = User.objects.filter(username= username).first()
if temp_username is not None:
print('This username already exists in our database! Please try another username.')
return JsonResponse({'result' : -1})
password = body['password']
user_obj=User.objects.create(email=email,username=username, password=password)
user_obj.save()
if user_obj is not None:
base64username = base64.b64encode( bytes(username, "utf-8") )
base64username_string = base64username.decode("utf-8")
print('Success!')
email_subject = "Welcome to TwitClone!"
email_from = "me@gmail.com"
email_to = [user_obj.email]
email_content = "Hello there! Thanks for joining the TwitClone Community!\n\nPlease click on this link to activate your account: http://127.0.0.1:8000/myapp/activate/"+base64username_string
x = send_mail(subject = email_subject, from_email = email_from, recipient_list = email_to, message = email_content, fail_silently=False)
return JsonResponse({'result' : 1})
else:
print('There was an error. :(')
return JsonResponse({'result' : -1})
else:
print('There was an error. :(')
return JsonResponse({'result' : -1})
我也有更多的类和视图,但是我只是将这两个类和视图放在一起,以提供对我如何编码项目的理解。
如何正确测试此注册查看过程?