def register(request): if request.method != 'POST': form = UserCreationFormBoot() else: form = UserCreationFormBoot(data=request.POST) if form.is_valid(): user_storage = ORMUserStorage() user_controller = UserController(user_storage) queue_storage = ORMQueueStorage() queue_controller = QueueController(queue_storage) new_user = form.save() authenticated_user = authenticate( username=new_user.username, password=request.POST['password1'] ) user_controller.add_user( nick=new_user.username, uid=new_user.id ) user = user_storage.get_user_by_nick(nick=new_user.username) queue = queue_controller.add_queue(name='DEFAULT', key=os.urandom(6).hex(), owner=user) user_controller.link_user_with_queue(user, queue) login(request, authenticated_user) return HttpResponseRedirect(reverse('web_tracker:index')) context = {'form': form} return render(request, 'users/register.html', context)
class UserTests(TestCase): def setUp(self): self.storage = TestUserStorage([]) self.controller = UserController(self.storage) def test_add_user(self): user = self.controller.add_user(nick='tester', uid=0) self.assertIn(user, self.storage.users) def test_find_user_by_nick(self): user_1 = self.controller.add_user(nick='user 1', uid=0) igor = self.controller.add_user(nick='igor', uid=1) vanya = self.controller.add_user(nick='vanya', uid=2) found = self.controller.find_user(nick='igor') self.assertEqual(igor, found) def test_find_user_by_uid(self): user_1 = self.controller.add_user(nick='user 1', uid=0) igor = self.controller.add_user(nick='igor', uid=1) vanya = self.controller.add_user(nick='vanya', uid=2) found = self.controller.find_user(uid=2) self.assertEqual(vanya, found) def test_clear_user_notifications(self): user = self.controller.add_user(nick='tester', uid=0) user.notifications.append('Hello') user.notifications.append('I need to work') user.notifications.append('Go home') self.controller.clear_user_notifications(user) self.assertListEqual(user.notifications, []) def test_clear_user_2_old_notifications(self): user = self.controller.add_user(nick='tester', uid=0) user.notifications.append('Hello') user.notifications.append('I need to work') user.notifications.append('Go home') self.controller.clear_user_notifications(user, 2) self.assertListEqual(user.notifications, ['Go home']) def test_clear_new_messages(self): user = self.controller.add_user(nick='tester', uid=0) user.notifications.append('other notification') user.new_messages.append('1 new notify') user.new_messages.append('2 new notify') self.controller.clear_new_messages(user) self.assertListEqual(user.notifications, ['other notification', '1 new notify', '2 new notify']) self.assertListEqual(user.new_messages, []) def test_notify_user(self): user = self.controller.add_user(nick='tester', uid=0) self.controller.notify_user(user, 'privet', show_time=False) self.controller.notify_user(user, 'with time') expected_str = ''.join([dt.now().strftime(Time.EXTENDED_TIME_FORMAT), ': ', 'with time']) self.assertListEqual(user.new_messages, ['privet', expected_str]) def test_link_user_with_queue(self): user = self.controller.add_user(nick='tester', uid=0) queue = Queue('test queue', key='0', owner=user.nick) self.controller.link_user_with_queue(user, queue) self.assertListEqual(user.queues, ['0']) def test_unlink_user_with_queue(self): user = self.controller.add_user(nick='tester', uid=0) queue = Queue('test queue', key='0', owner=user.nick) self.controller.link_user_with_queue(user, queue) self.controller.unlink_user_and_queue(user, queue) self.assertListEqual(user.queues, []) def test_link_responsible_with_task(self): owner = self.controller.add_user(nick='owner', uid=0) responsible = self.controller.add_user(nick='responsible', uid=1) task = Task(name='Test task', key='3453', author=owner.nick) self.controller.link_responsible_with_task(responsible, task) self.assertListEqual(responsible.tasks_responsible, ['3453']) def test_unlink_responsible_with_task(self): owner = self.controller.add_user(nick='owner', uid=0) responsible = self.controller.add_user(nick='responsible', uid=1) task = Task(name='Test task', key='3453', author=owner.nick) self.controller.link_responsible_with_task(responsible, task) self.controller.unlink_responsible_and_task(responsible, task) self.assertListEqual(responsible.tasks_responsible, []) def test_link_author_with_task(self): owner = self.controller.add_user(nick='owner', uid=0) task = Task(name='Test task', key='3453', author=owner.nick) self.controller.link_author_with_task(owner, task) self.assertListEqual(owner.tasks_author, ['3453']) def test_unlink_author_with_task(self): owner = self.controller.add_user(nick='owner', uid=0) task = Task(name='Test task', key='3453', author=owner.nick) self.controller.link_author_with_task(owner, task) self.controller.unlink_author_and_task(owner, task) self.assertListEqual(owner.tasks_author, [])