Beispiel #1
0
    def test_wechat_qr_login(self, mock_get_union_id):
        mock_get_union_id.return_value = 'test_wechat_user_id'

        wechat_config = WechatConfig.get_current()
        wechat_config.__dict__.update(appid='test_appid',
                                      secret='test_secret',
                                      qr_app_valid=True)
        wechat_config.save()

        user = User.objects.create(username='******',
                                   password='******',
                                   name='张三',
                                   mobile='18812341234')
        user.save()

        unionid = 'test_wechat_user_id'
        wechat_user = WechatUser.valid_objects.create(unionid=unionid,
                                                      user=user)
        wechat_user.save()

        client = self.client
        res = client.get(
            reverse('siteapi:wechat_qr_callback') + '?code=test_auth_code')

        self.assertEqual(res.status_code, 200)
        self.assertIn('token', res.json())
Beispiel #2
0
 def test_wechat_qr_login_newuser(self, mock_get_union_id):  # pylint: disable=invalid-name
     wechat_config = WechatConfig.get_current()
     wechat_config.__dict__.update(app_id='test_app_id', app_private_key='test_app_private_key', \
                                   wechat_public_key='test_wechat_public_key', qr_app_valid=True)
     wechat_config.save()
     mock_get_union_id.return_value = ''
     client = self.client
     res = client.get(
         reverse('siteapi:wechat_qr_callback') + '?code=test_auth_code')
     expect = {'token': '', 'third_party_id': ''}
     self.assertEqual(res.json(), expect)
Beispiel #3
0
 def test_wechat_qr_login_forbidden(self):  # pylint: disable=invalid-name
     client = self.client
     wechat_config = WechatConfig.get_current()
     wechat_config.__dict__.update(app_id='app_id', app_private_key='app_private_key', \
                                   wechat_public_key='wechat_public_key', qr_app_valid=False)
     wechat_config.save()
     res = client.get(reverse('siteapi:wechat_qr_callback'), \
                       data={'code': 'test_auth_code'})
     expect_json = {'err_msg': 'wechat qr not allowed'}
     expect_code = 403
     self.assertEqual(res.json(), expect_json)
     self.assertEqual(res.status_code, expect_code)
Beispiel #4
0
    def setUp(self):
        super().setUp()
        account_config = AccountConfig.get_current()
        account_config.allow_email = True
        account_config.allow_mobile = True
        account_config.allow_register = True
        account_config.allow_wechat_qr = True
        account_config.save()

        wechat_config = WechatConfig.get_current()
        wechat_config.qr_app_valid = True
        wechat_config.save()
Beispiel #5
0
    def test_meta(self):
        account_config = AccountConfig.get_current()
        account_config.allow_ding_qr = True
        account_config.allow_alipay_qr = True
        account_config.allow_qq_qr = True
        account_config.allow_work_wechat_qr = True
        account_config.allow_wechat_qr = True
        account_config.save()
        company_config = CompanyConfig.get_current()
        company_config.fullname_cn = "demo"
        company_config.save()
        ding_config = DingConfig.get_current()
        ding_config.corp_id = "corp_id"
        ding_config.qr_app_id = 'qr_app_id'
        ding_config.qr_app_valid = True
        ding_config.save()

        alipay_config = AlipayConfig.get_current()
        alipay_config.app_id = 'test_app_id'
        alipay_config.qr_app_valid = True
        alipay_config.save()

        qq_config = QQConfig.get_current()
        qq_config.app_id = 'test_app_id'
        qq_config.redirect_uri = 'test_redirect_uri'
        qq_config.qr_app_valid = True
        qq_config.save()

        work_wechat_config = WorkWechatConfig.get_current()
        work_wechat_config.corp_id = 'test_corp_id'
        work_wechat_config.agent_id = 'test_agent_id'
        work_wechat_config.qr_app_valid = True
        work_wechat_config.save()

        wechat_config = WechatConfig.get_current()
        wechat_config.appid = 'test_appid'
        wechat_config.secret = 'test_secret'
        wechat_config.qr_app_valid = True
        wechat_config.save()

        contactsconfig = ContactsConfig.get_current()
        contactsconfig.is_show = True
        contactsconfig.save()

        res = self.anonymous.get(reverse('siteapi:meta'))
        expect = {
            'company_config': {
                'name_cn': '',
                'fullname_cn': 'demo',
                'name_en': '',
                'fullname_en': '',
                'icon': '',
                'address': '',
                'domain': '',
                'display_name': 'demo',
                'color': '',
            },
            'ding_config': {
                'corp_id': 'corp_id',
                'app_key': '',
                'qr_app_id': 'qr_app_id',
            },
            'account_config': {
                'support_email': False,
                'support_mobile': False,
                'support_email_register': False,
                'support_mobile_register': False,
                'support_ding_qr': True,
                'support_alipay_qr': True,
                'support_qq_qr': True,
                'support_work_wechat_qr': True,
                'support_wechat_qr': True,
            },
            'alipay_config': {
                'app_id': 'test_app_id',
            },
            'qq_config': {
                'app_id': 'test_app_id',
            },
            'work_wechat_config': {
                'corp_id': 'test_corp_id',
                'agent_id': 'test_agent_id'
            },
            'wechat_config': {
                'appid': 'test_appid'
            },
            "contacts_config": {
                "is_show": True
            }
        }
        self.assertEqual(res.json(), expect)
Beispiel #6
0
    def update(self, instance, validated_data):  # pylint: disable=too-many-locals, too-many-statements, too-many-branches
        company_config_data = validated_data.pop('company_config', None)
        if company_config_data:
            if not Dept.valid_objects.filter(parent__uid='root').exists():
                uid = gen_uid(name=company_config_data.get('name_cn', ''),
                              cls=Dept)
                parent_dept = Dept.valid_objects.filter(uid='root').first()
                cli = CLI()
                dept_data = {
                    'parent_uid': 'root',
                    'name': company_config_data.get('name_cn', ''),
                    'uid': uid,
                }
                child_dept = cli.create_dept(dept_data)
                cli.add_dept_to_dept(child_dept, parent_dept)
            else:
                company_dept = Dept.valid_objects.filter(
                    parent__uid='root').first()
                company_dept.name = company_config_data.get('name_cn', '')
                company_dept.save()
            serializer = CompanyConfigSerializer(CompanyConfig.get_current(),
                                                 company_config_data)
            serializer.is_valid(raise_exception=True)
            serializer.save()

        account_config_data = validated_data.pop('account_config', None)
        if account_config_data:
            serializer = AccountConfigSerializer(AccountConfig.get_current(),
                                                 account_config_data,
                                                 partial=True)
            serializer.is_valid(raise_exception=True)
            serializer.save()

        ding_config_data = validated_data.pop('ding_config', None)
        if ding_config_data:
            serializer = DingConfigSerializer(DingConfig.get_current(),
                                              ding_config_data,
                                              partial=True)
            serializer.is_valid(raise_exception=True)
            serializer.save()

        sms_config_data = validated_data.pop('sms_config', None)
        if sms_config_data:
            access_secret = sms_config_data.pop('access_secret', '')
            config = SMSConfig.get_current()
            serializer = SMSConfigSerializer(config,
                                             sms_config_data,
                                             partial=True)
            serializer.is_valid(raise_exception=True)  # pylint: disable=not-callable
            config.__dict__.update(serializer.validated_data)

            if access_secret:
                config.access_secret = access_secret
            if not config.check_valid():
                raise ValidationError({'sms': ['invalid']})
            config.is_valid = True
            config.save()

        email_config_data = validated_data.pop('email_config', None)
        if email_config_data:
            access_secret = email_config_data.pop('access_secret', '')
            config = EmailConfig.get_current()
            serializer = EmailConfigSerializer(config,
                                               email_config_data,
                                               partial=True)
            serializer.is_valid(raise_exception=True)  # pylint: disable=not-callable
            config.__dict__.update(serializer.validated_data)

            if access_secret:
                config.access_secret = access_secret
            if not config.check_valid():
                raise ValidationError({'email': ['invalid']})
            config.is_valid = True
            config.save()

        alipay_config_data = validated_data.pop('alipay_config', None)
        if alipay_config_data:
            if alipay_config_data["app_id"] != '':
                serializer = AlipayConfigSerializer(AlipayConfig.get_current(),
                                                    alipay_config_data,
                                                    partial=True)
                serializer.is_valid(raise_exception=True)
                serializer.save()

        qq_config_data = validated_data.pop('qq_config', None)
        if qq_config_data:
            serializer = QQConfigSerializer(QQConfig.get_current(),
                                            qq_config_data,
                                            partial=True)
            serializer.is_valid(raise_exception=True)
            serializer.save()

        work_wechat_config_data = validated_data.pop('work_wechat_config',
                                                     None)
        if work_wechat_config_data:
            serializer = WorkWechatConfigSerializer(WorkWechatConfig.get_current(),\
                work_wechat_config_data, partial=True)
            serializer.is_valid(raise_exception=True)
            serializer.save()

        wechat_config_data = validated_data.pop('wechat_config', None)
        if wechat_config_data:
            serializer = WechatConfigSerializer(WechatConfig.get_current(),\
                wechat_config_data, partial=True)
            serializer.is_valid(raise_exception=True)
            serializer.save()

        password_config_data = validated_data.pop('password_config', None)
        if password_config_data:
            config = PasswordComplexityConfig.get_current()
            serializer = PasswordConfigSerializer(config,
                                                  password_config_data,
                                                  partial=True)
            serializer.is_valid(raise_exception=True)  # pylint: disable=not-callable
            config.__dict__.update(serializer.validated_data)
            config.save()

        github_config = validated_data.pop('github_config', None)
        if github_config:
            config = GithubConfig.get_current()
            serializer = GithubConfigSerializer(config,
                                                github_config,
                                                partial=True)
            serializer.is_valid(raise_exception=True)
            serializer.save()

        instance.refresh_from_db()

        return instance