def register(request): args = {} args.update(csrf(request)) args['form'] = UserCreationForm() if request.POST: newuser_form = UserCreationForm(request.POST) if newuser_form.is_valid(): newuser_form.save() newuser = auth.authenticate(username=newuser_form.cleaned_data['username'], password=newuser_form.cleaned_data['password2']) auth.login(request, newuser) #Adding user to main bucket try: c = Bucket('couchbase://localhost/nihongo') username = newuser_form.cleaned_data['username'] password = newuser_form.cleaned_data['password2'] c_username = '******' + username new_user = {'username' : username, 'password' : password, 'doc_type' : 'user_doc'} doc_channels = [c_username] new_user['doc_channels'] = doc_channels c.upsert(c_username, new_user) except CouchbaseError as ce: raise Http404("Couchbase server error") #Adding user to sync gateway database SyncGateway.put_user(c_username, '*****@*****.**', password, [c_username]) return redirect('/') else: args['form'] = newuser_form return render_to_response('register.html', args)
def confirm_ir(request, ir_id, group_id, user_id): print("IR CONFIRMATION") c = Bucket('couchbase://localhost/nihongo') user_doc = c.get(user_id).value password = user_doc['password'] ir_doc = c.get(ir_id).value if 'accept' in request.POST: print("IR ACCEPTED") ir_doc['confirmed'] = 'accepted' sync_user = SyncGateway.get_user(user_id) new_sync_user = {} admin_channels = sync_user['admin_channels'] #all_channels = sync_user['all_channels'] admin_channels.append(group_id) SyncGateway.put_user(sync_user['name'], '*****@*****.**', password, admin_channels) print(sync_user) elif 'decline' in request.POST: ir_doc['confirmed'] = 'declined' print("IR DECLINED") c.upsert(ir_id, ir_doc) return HttpResponseRedirect(reverse('tutor:tutor_groups'))
def test_create_session(self): SyncGateway.put_user("username1", "*****@*****.**", "password", ["public"]) res = SyncGateway.create_session("username1") self.assertEqual(res.status_code, 200) res = SyncGateway.create_session("username1", ttl=60 * 60 * 24) self.assertEqual(res.status_code, 200)
def test_delete_user(self): res = SyncGateway.put_user("username1", "*****@*****.**", "password", ["public"]) res = SyncGateway.delete_user("username1") self.assertTrue(res) with self.assertRaises(SyncGatewayException): SyncGateway.get_user("*****@*****.**")
def test_bug_with_load_by_uid(self): # it sets uid value to first textfield uid = 'unfilled_model_uid' incomplete_data = dict(title2='title') SyncGateway.save_json(uid, incomplete_data) m = Mock(uid) self.assertNotEqual(uid, m.title) self.assertFalse(bool(m.title))
def test_bug_with_load_by_uid(self): # it sets uid value to first textfield uid = "unfilled_model_uid" incomplete_data = dict(title2="title") SyncGateway.save_json(uid, incomplete_data) m = Mock(uid) self.assertNotEqual(uid, m.title) self.assertFalse(bool(m.title))
def handle(self, *args, **options): latest_modification_time = timezone.now() - timedelta(days=30) date_to = latest_modification_time.date().isoformat() for uid in self.get_uids(date_to): o = CouchbaseModel(uid) # print 'will delete %s / %s' % (uid, o.rev) SyncGateway.delete_document(uid, o.rev)
def test_change_username(self): res = SyncGateway.put_user("*****@*****.**", "*****@*****.**", "password", ["public", "boo"]) res = SyncGateway.change_username("*****@*****.**", "*****@*****.**", "password") self.assertTrue(res) d = SyncGateway.get_user("*****@*****.**") self.assertEqual(d["name"], "*****@*****.**") self.assertEqual(d["email"], "*****@*****.**") self.assertIn("boo", d["admin_channels"]) with self.assertRaises(SyncGatewayException): SyncGateway.get_user("*****@*****.**")
def test_change_username(self): res = SyncGateway.put_user("*****@*****.**", "*****@*****.**", "password", ["public", 'boo']) res = SyncGateway.change_username("*****@*****.**", "*****@*****.**", "password") self.assertTrue(res) d = SyncGateway.get_user("*****@*****.**") self.assertEqual(d['name'], "*****@*****.**") self.assertEqual(d['email'], "*****@*****.**") self.assertIn('boo', d['admin_channels']) with self.assertRaises(SyncGatewayException): SyncGateway.get_user("*****@*****.**")
def clean_buckets(): if "test" not in settings.COUCHBASE_BUCKET: raise Exception("will not clean non-test bucket") all_docs = SyncGateway.all_docs([], really_all=True) for i in all_docs["rows"]: uid = i["id"] rev = i["value"]["rev"] if "sync" in uid: continue # print 'del %s' % uid SyncGateway.delete_document(uid, rev)
def test_remove_channels(self): c = str(uuid4()) new_c1 = str(uuid4()) new_c2 = str(uuid4()) username = "******" SyncGateway.put_user(username, "*****@*****.**", "password", ["public", c, new_c1, new_c2]) SyncGateway.remove_channels(username, [new_c1, new_c2]) d = SyncGateway.get_user(username) self.assertIn(c, d["admin_channels"]) self.assertIn("public", d["admin_channels"]) self.assertNotIn(new_c1, d["admin_channels"]) self.assertNotIn(new_c2, d["admin_channels"]) self.assertEqual(2, len(d["admin_channels"]))
def clean_buckets(): if 'test' not in settings.COUCHBASE_BUCKET: raise Exception('will not clean non-test bucket') all_docs = SyncGateway.all_docs([], really_all=True) for i in all_docs['rows']: uid = i['id'] rev = i['value']['rev'] if 'sync' in uid: continue # print 'del %s' % uid SyncGateway.delete_document(uid, rev)
def test_remove_channels(self): c = str(uuid4()) new_c1 = str(uuid4()) new_c2 = str(uuid4()) username = '******' SyncGateway.put_user(username, "*****@*****.**", "password", ["public", c, new_c1, new_c2]) SyncGateway.remove_channels(username, [new_c1, new_c2]) d = SyncGateway.get_user(username) self.assertIn(c, d['admin_channels']) self.assertIn('public', d['admin_channels']) self.assertNotIn(new_c1, d['admin_channels']) self.assertNotIn(new_c2, d['admin_channels']) self.assertEqual(2, len(d['admin_channels']))
def test_append_channels(self): c = str(uuid4()) new_c1 = str(uuid4()) new_c2 = str(uuid4()) username = '******' SyncGateway.put_user( username, "*****@*****.**", "password", ["public", c] ) SyncGateway.append_channels(username, [new_c1, new_c2]) d = SyncGateway.get_user(username) self.assertIn(c, d['admin_channels']) self.assertIn('public', d['admin_channels']) self.assertIn(new_c1, d['admin_channels']) self.assertIn(new_c2, d['admin_channels']) self.assertEqual(4, len(d['admin_channels']))
def setUp(self): from django_cbtools.management.commands.create_cb_views import Command SyncGateway.put_admin_user() command = Command() command.handle() # get random string to use as channel fake = Mock() self.channel = fake.get_uid() # print self.channel m = Mock(title="my title", b=True, num=12) m.channels.append(self.channel) m.save() self.uid1 = m.uid m = Mock(title="my title", b=True, num=12) m.channels.append(self.channel) m.save() self.uid2 = m.uid
def test_create_user(self): res = SyncGateway.put_user("username1", "*****@*****.**", "password", ["public"]) self.assertTrue(res)
def setUp(self): SyncGateway.put_admin_user() clean_buckets() self.maxDiff = None
def handle(self, *args, **options): SyncGateway.put_admin_user() SyncGateway.put_guest_user()
def test_st_deleted_new_object(self): uid = 'some_key' data = dict(title2='title') SyncGateway.save_json(uid, data) m = Mock(uid) self.assertFalse(m.st_deleted)
def test_get_user(self): SyncGateway.put_user("*****@*****.**", "*****@*****.**", "password", ["public"]) d = SyncGateway.get_user("*****@*****.**") self.assertEqual(d['name'], "*****@*****.**") self.assertEqual(d['email'], "*****@*****.**")
def test_create_session_for_non_existing_user(self): with self.assertRaises(SyncGatewayException): SyncGateway.create_session("not_existing_user")
def setUp(self): SyncGateway.put_admin_user() clean_buckets()
def test_get_user(self): SyncGateway.put_user("*****@*****.**", "*****@*****.**", "password", ["public"]) d = SyncGateway.get_user("*****@*****.**") self.assertEqual(d["name"], "*****@*****.**") self.assertEqual(d["email"], "*****@*****.**")
def test_st_deleted_new_object(self): uid = "some_key" data = dict(title2="title") SyncGateway.save_json(uid, data) m = Mock(uid) self.assertFalse(m.st_deleted)