class ConsumerTest(TestCase): fixtures = ['models.json'] def setUp(self): self.consumer = Consumer() self.consumer.name = "Piston Test Consumer" self.consumer.description = "A test consumer for Piston." self.consumer.user = User.objects.get(pk=3) self.consumer.generate_random_codes() def test_create_pending(self): """ Ensure creating a pending Consumer sends proper emails """ # If it's pending we should have two messages in the outbox; one # to the consumer and one to the site admins. if len(settings.ADMINS): self.assertEquals(len(mail.outbox), 2) else: self.assertEquals(len(mail.outbox), 1) expected = "Your API Consumer for example.com is awaiting approval." self.assertEquals(mail.outbox[0].subject, expected) def test_delete_consumer(self): """ Ensure deleting a Consumer sends a cancel email """ # Clear out the outbox before we test for the cancel email. mail.outbox = [] # Delete the consumer, which should fire off the cancel email. self.consumer.delete() self.assertEquals(len(mail.outbox), 1) expected = "Your API Consumer for example.com has been canceled." self.assertEquals(mail.outbox[0].subject, expected)
class ConsumerTest(TestCase): fixtures = ['models.json'] def setUp(self): self.consumer = Consumer() self.consumer.name = "Piston Test Consumer" self.consumer.description = "A test consumer for Piston." self.consumer.user = User.objects.get(pk=3) self.consumer.generate_random_codes() def _pre_test_email(self): template = "piston/mails/consumer_%s.txt" % self.consumer.status try: loader.render_to_string(template, { 'consumer': self.consumer, 'user': self.consumer.user }) return True except TemplateDoesNotExist: """ They haven't set up the templates, which means they might not want these emails sent. """ return False def test_create_pending(self): """ Ensure creating a pending Consumer sends proper emails """ # Verify if the emails can be sent if not self._pre_test_email(): return # If it's pending we should have two messages in the outbox; one # to the consumer and one to the site admins. if len(settings.ADMINS): self.assertEquals(len(mail.outbox), 2) else: self.assertEquals(len(mail.outbox), 1) expected = "Your API Consumer for example.com is awaiting approval." self.assertEquals(mail.outbox[0].subject, expected) def test_delete_consumer(self): """ Ensure deleting a Consumer sends a cancel email """ # Clear out the outbox before we test for the cancel email. mail.outbox = [] # Delete the consumer, which should fire off the cancel email. self.consumer.delete() # Verify if the emails can be sent if not self._pre_test_email(): return self.assertEquals(len(mail.outbox), 1) expected = "Your API Consumer for example.com has been canceled." self.assertEquals(mail.outbox[0].subject, expected)
def consumer_create(request): if request.method == "POST": form = ConsumerCreateForm(request.POST) if form.is_valid(): consumer = Consumer() consumer.name = form.cleaned_data['name'] consumer.description = form.cleaned_data['description'] consumer.user_id = request.user.username consumer.refresh_key_secret() consumer.save() token = Token() token.key = Token.generate_token() token.secret = Token.generate_token() token.consumer = consumer token.user = request.user.username token.type = 'A' token.save() return HttpResponseRedirect('/accounts/profile') else: form = ConsumerCreateForm() params = {'form': form} return render_to_response('oauth/consumer_form.tpl', params, context_instance=RequestContext(request))
def change_password(): email = request.json.get('email', None) if not email or email == '': return None consumer = Consumer() consumer.email = email consumer = Consumer.query.filter_by(email=email).first() if not consumer: return jsonify({"msg": "This email is not registered"}), 404 token = generate_confirmation_token(consumer.email) confirm_url = 'http://localhost:3000/confirmation/' + token html = render_template('email_confirmation.html', confirm_url=confirm_url) subject = "Por favor, Confirmar su email." sendMail("Por favor, Confirmar su email.", consumer.email, html) return jsonify({"success": "Email send successfully"}), 200
def setUp(self): self.consumer = Consumer() self.consumer.name = "Piston Test Consumer" self.consumer.description = "A test consumer for Piston." self.consumer.user = User.objects.get(pk=3) self.consumer.generate_random_codes() self.consumer.save()
def get_oauthsecret_for_key(consumer_key): """ Retuns a the oauth consumer secret based on the consumer key :param consumer_key: the consumer key :return consumer_secret """ return Consumer.getsecretforkey(consumer_key)
def consume_history(message): try: user = Consumer.getBy(weixinId=message.source) if user == None: return template.bind_help spider = ApiClient() info = spider.get_cost_today(user.studentId, user.password) status = info.get('errcode') cost = info.get('cost', '') detail = info.get('detail', '') balance = info.get('balance', '') import time user.lastQueryTime = time.strftime("%Y-%m-%d %H:%M:%S") user.update() if status == 0: return [[ template.page["consume_today"].title, template.page["consume_today"].description.format(today=cost, balance=balance) \ + ('\n\n详细消费记录如下: \n\n' + detail if detail else ""), template.page["consume_today"].img, template.page["consume_today"].url, ]] else: return "密码已变更, 身份验证出错." except Exception: return '抱歉,远程服务未启动, 请稍后再试.'
def queries_verify(message): content = str(message.content).strip() if re.match('绑定.*', content): s = content.replace('绑定', '').strip() try: row = s.split(' ', 1) if len(row) < 2: return template.help_message user = Consumer.getBy(weixinId=message.source) username = row[0].upper() password = row[1] flg = ApiClient().verify(username, password) if flg is None: return '抱歉, 远程服务未启动, 请稍后再试.' elif not flg: return '身份验证失败, 请检查用户名或密码.' if user is None: Consumer(dict( weixinId=message.source, studentId=username, password=password, )).insert() else: user.studentId = username user.password = password user.update() return '绑定成功, 请通过菜单进行消费记录查询 :-)' except Exception, e: return "抱歉, 出现错误." return str(e)
def lookup_consumer(self, key): consumers = Consumer.all().filter("key_ =",key).fetch(1000) if len(consumers) == 1: self.consumer = consumers[0] return self.consumer elif len(consumers) == 0: return None else: raise Exception('More then one consumer matches Consumer key "%s"'%key)
def register(): email = request.json.get('email') password = request.json.get('password') if not email: return jsonify({"error": "You need to write yor email"}), 422 if not password: return jsonify({"error": "You need to write your password"}), 422 consumer = Consumer.query.filter_by(email=email).first() if consumer: return jsonify({"error": "This email already exist"}), 422 consumer = Consumer() consumer.email = email consumer.password = bcrypt.generate_password_hash(password) db.session.add(consumer) db.session.commit() if bcrypt.check_password_hash(consumer.password, password): access_token = create_access_token(identity=consumer.email) data = {"access_token": access_token, "consumer": consumer.serialize()} return jsonify(data), 200 else: return jsonify({"error": "Email or password are incorrect"}), 401
def setUp(self): self.consumer = Consumer() self.consumer.name = "Piston Test Consumer" self.consumer.description = "A test consumer for Piston." self.consumer.user = User.objects.get(pk=3) self.consumer.generate_random_codes()
weixinId=message.source, studentId=username, password=password, )).insert() else: user.studentId = username user.password = password user.update() return '绑定成功, 请通过菜单进行消费记录查询 :-)' except Exception, e: return "抱歉, 出现错误." return str(e) elif re.match('挂失饭卡', content): try: user = Consumer.getBy(weixinId=message.source) if user is None: return '操作失败, 请先绑定您的饭卡.' result = ApiClient().report_loss(user.studentId, user.password) if result is None: return '抱歉, 远程服务未启动, 请稍后再试.' if result.get('errcode', 100) != 0: return '出现错误' if result.get('errmsg', '') == '': return '出现错误' return result.get('errmsg', '') except Exception, e: return str(e) elif re.match('绑定查询', content):