Пример #1
0
 def extract_company_info(self, company_url, company_name, position):
     if not company_url:
         return
     res = self.send_requests(company_url)
     document = get_document(res.text)
     name = get_simple_dom(document, './/div[@class="tHeader tHCop"]/div[contains(@class, "in")]/h1/@title')
     # 使用前程无忧搭建自己公司门户的公司
     if not name:
         return Company.insert(name=company_name,
                               uniqueId=self.get_company_unique_id(company_url),
                               position=position,
                               companyUrl=company_url,
                               site=self.task.site).execute()
     # 普通公司
     base_info = get_simple_dom(document, './/p[@class="ltype"]/text()', '')
     infos = base_info.split('|')
     nature = infos[0].strip() if len(infos) > 0 else ''
     scale = infos[1].strip() if len(infos) > 1 else ''
     category = infos[2].strip() if len(infos) > 2 else ''
     position_info = get_richtext(document, './/div[@class="inbox"]/p[@class="fp"]')
     position_info = position_info.strip().replace('公司地址:', '')
     position = re.sub('(\(邮编:.*\))', '', position_info) or ''
     position = position.strip()
     description = get_richtext(document, './/div[@class="con_txt"]')
     return Company.insert(name=name,
                           nature=nature,
                           scale=scale,
                           category=category,
                           position=position,
                           description=description,
                           uniqueId=self.get_company_unique_id(company_url),
                           companyUrl=company_url,
                           site=self.task.site).execute()
Пример #2
0
    def add_company(payload):
        body = request.json

        # Need to have name and website keys in body
        if not all([x in body for x in ['name', 'website']]):
            abort(422)

        # Here we want this to return None otherwise another company has that name
        # If you don't handle this way, function works, but unittests catches the
        # print statement below in the 'except Exception as e' block and clutters
        # up the unittests output.
        duplicate_name = Company.query.filter_by(
            name=body['name'].strip()).one_or_none()
        if duplicate_name:
            abort(422)
        duplicate_website = Company.query.filter_by(
            website=body['website'].strip()).one_or_none()
        if duplicate_website:
            abort(422)

        try:
            new_co = Company(name=body['name'].strip(),
                             website=body['website'].strip())
            new_co.insert()
        except Exception as e:
            print(f'Exception in add_company(): {e}')
            abort(422)  # Syntax is good, can't process for semantic reasons

        return jsonify({"id": new_co.id, "success": True})
Пример #3
0
    def create_company(jwt):
        request_value = request.get_json()
        if request_value is None:
            abort(400)

        properties = ['name', 'description', 'imageBase64', 'state', 'city']
        for prop in properties:
            if prop not in request_value:
                abort(400)

        if request_value['name'] is None:
            abort(400)

        if request_value['state'] is None or request_value['city'] is None:
            abort(400)

        company = Company(
            request_value['name'],
            request_value['description'],
            request_value['imageBase64'],
            request_value['state'],
            request_value['city']
        )
        company.insert()

        return jsonify({
            'status_code': 200,
            'company': company.format(),
            'message': 'The company was successfully created',
            'success': True,
        })
Пример #4
0
    def test_delete_company(self):
        """Attempts to delete a company successfully as Client."""
        # Create a test company to delete
        new_co = Company(name="we're toast, LLC",
                         website="dooooooooooommmmed.com")
        new_co.insert()
        new_co_id = new_co.id

        # Make sure it added successfully
        all_companies = Company.query.all()
        self.assertEqual(len(all_companies), 4)  # 3 originally in test DB

        # Delete it through route
        res = self.client().delete(f'/company/{new_co_id}',
                                   headers=self.headers_client)
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'], True)
        self.assertEqual(data['id'], new_co_id)
Пример #5
0
 def extract_company_info(self, company_url, company_name, position):
     res = self.send_requests(company_url)
     document = get_document(res.text)
     name = get_simple_dom(document, './/div[@class="mainLeft"]/div/h1/text()', '').strip()
     if not name:
         return Company.insert(site=self.task.site,
                               name=company_name,
                               uniqueId=self.get_company_unique_id(company_url),
                               companyUrl=company_url,
                               position=position).execute()
     for item in document.xpath('.//table[@class="comTinyDes"]/tbody/tr'):
         td_list = item.xpath('.//td')
         key = get_simple_dom(td_list, './/span/text()')
Пример #6
0
    def add_company(payload):
        try:
            body = request.get_json()

            new_name = body.get('name')
            new_industry = body.get('industry')
            new_employee = body.get('employee')
            new_city = body.get('city')
            new_region = body.get('region')
            new_address = body.get('address')
            new_email = body.get('email')
            new_phone = body.get('phone')
            new_logo_link = body.get('logo_link')
            new_facebook_link = body.get('facebook_link')
            new_website_link = body.get('website_link')
            new_description = body.get('description')
            new_seeking_employee = body.get('seeking_employee')

            company = Company(name=new_name,
                              industry=new_industry,
                              employee=new_employee,
                              city=new_city,
                              region=new_region,
                              address=new_address,
                              email=new_email,
                              phone=new_phone,
                              logo_link=new_logo_link,
                              facebook_link=new_facebook_link,
                              website_link=new_website_link,
                              description=new_description,
                              seeking_employee=new_seeking_employee)

            company.insert()
            return jsonify({'success': True})
        except Exception:
            print(sys.exc_info())
            abort(422)
Пример #7
0
class CapstoneTestCase(unittest.TestCase):
    def setUp(self):
        database_name = 'capstone_test'
        database_path = 'postgresql://jaimeaznar@{}/{}'.format(
            'localhost:5432', database_name)
        self.app = create_app()
        self.app.config['WTF_CSRF_ENABLED'] = False
        self.client = self.app.test_client
        setup_db(self.app, database_path)

        db.session.close()
        db.drop_all()
        db.create_all()

        # mock company
        self.company = Company(name='test company',
                               city='test city',
                               state='test state',
                               address='test address',
                               phone='12345')
        self.company.insert()

        # mock product
        self.product = Product(name='test product',
                               image='static/img/tomahawk.jpg',
                               description='test description',
                               company_id=1)
        self.product.insert()

    def tearDown(self):
        pass

#----------------------------------------------------------------------------#
#  Tests
#----------------------------------------------------------------------------#
# Test index page

    def test_index(self):

        response = self.client().get('/')
        self.assertEqual(response.status_code, 200)

    ################
    # Product
    ################
    # Test products page
    def test_product(self):
        response = self.client().get('/api/products')
        data = json.loads(response.data)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(data['success'], True)

    # Test search product by name
    def test_search_product(self):
        print('test_search_product')
        response = self.client().get(
            '/api/products/search',
            query_string=dict(search_term='test product'))
        data = json.loads(response.data)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(data['success'], True)

    # Test search product by name not found

    def test_search_product_not_found(self):
        print('test_search_product_not_found')
        response = self.client().get('/api/products/search',
                                     query_string=dict(search_term='facebook'))
        data = json.loads(response.data)

        self.assertEqual(response.status_code, 404)
        self.assertEqual(data['success'], False)

    # Test search product by id
    def test_search_products_by_id(self):
        response = self.client().get('/api/products/1')
        data = json.loads(response.data)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(data['success'], True)

    ################
    # Company
    ################
    # Test products page
    def test_company(self):
        response = self.client().get('/api/companies')
        data = json.loads(response.data)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(data['success'], True)

    # Test search product by name
    def test_search_company(self):

        response = self.client().get(
            '/api/companies/search',
            query_string=dict(search_term='test company'))
        data = json.loads(response.data)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(data['success'], True)

    # Test search company by name not found
    def test_search_company_not_found(self):
        response = self.client().get('/api/company/search',
                                     query_string=dict(search_term='facebook'))
        data = json.loads(response.data)

        self.assertEqual(response.status_code, 404)
        self.assertEqual(data['success'], False)

    # Test search company by id
    def test_search_company_by_id(self):
        response = self.client().get('/api/companies/1')
        data = json.loads(response.data)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(data['success'], True)

    ################
    # Create Company
    ################

    # Create company get form

    def test_form_create_company(self):
        response = self.client().get(
            '/api/companies/create',
            headers={'Authorization': 'Bearer ' + JWT_TOKEN})
        data = json.loads(response.data)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(data['success'], True)

    # Create company post
    def test_create_company(self):
        response = self.client().post(
            '/api/companies/create',
            data=dict(name='test name',
                      city='test city',
                      state='test state',
                      address='test address',
                      phone='phone'),
            headers={'Authorization': 'Bearer ' + JWT_TOKEN})
        data = json.loads(response.data)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(data['success'], True)
        self.assertTrue(len(data['company']))

# Create company post incomplete info

    def test_create_company_with_incomplete_data(self):

        response = self.client().post(
            '/api/companies/create',
            data=dict(name='test name', city='test city'),
            headers={'Authorization': 'Bearer ' + JWT_TOKEN})
        data = json.loads(response.data)

        self.assertEqual(response.status_code, 400)
        self.assertEqual(data['success'], False)

    # Create company post without token
    def test_create_company_without_token(self):
        response = self.client().post('/api/companies/create',
                                      data=dict(name='test name',
                                                city='test city',
                                                state='test state',
                                                address='test address',
                                                phone='phone'))
        data = json.loads(response.data)

        self.assertEqual(response.status_code, 401)
        self.assertEqual(data['success'], False)


#     ################
#     # Create Product
#     ################

# Create product form

    def test_form_create_product(self):
        response = self.client().get(
            '/api/products/create',
            headers={'Authorization': 'Bearer ' + JWT_TOKEN})
        data = json.loads(response.data)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(data['success'], True)

    # Create product post
    def test_create_product(self):
        path_img = 'static/img/tomahawk.jpg'
        with open(path_img, 'rb') as img:

            response = self.client().post(
                '/api/products/create',
                data=dict(
                    name='test name',
                    image=img,
                    description='test description',
                    company=self.company.id,
                ),
                headers={'Authorization': 'Bearer ' + JWT_TOKEN})
        data = json.loads(response.data)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(data['success'], True)
        self.assertTrue(len(data['new_product']))

    # ################
    # # Patch Company
    # ################
    #  Patch company get form
    def test_form_patch_company(self):
        # Create company get form
        response = self.client().get(
            '/api/companies/1/edit',
            headers={'Authorization': 'Bearer ' + JWT_TOKEN})
        data = json.loads(response.data)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(data['success'], True)

    # Patch company post
    def test_patch_company(self):
        response = self.client().patch(
            '/api/companies/1/edit',
            data=dict(name='patch name',
                      city='patch city',
                      state='patch state',
                      address='patch address',
                      phone='patch phone'),
            headers={'Authorization': 'Bearer ' + JWT_TOKEN})
        data = json.loads(response.data)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(data['success'], True)

    # Patch company without token
    def test_patch_company_without_token(self):
        response = self.client().patch('/api/companies/1/edit',
                                       data=dict(name='patch name',
                                                 city='patch city',
                                                 state='patch state',
                                                 address='patch address',
                                                 phone='patch phone'))
        data = json.loads(response.data)
        self.assertEqual(response.status_code, 401)
        self.assertEqual(data['success'], False)

    # ################
    # # Delete Company
    # ################

    # Delete company
    def test_delete_company(self):
        response = self.client().delete(
            '/api/companies/1/delete',
            headers={'Authorization': 'Bearer ' + JWT_TOKEN})
        data = json.loads(response.data)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(data['success'], True)

    # Delete with invalid id
    def test_delete_company_with_invalid_id(self):
        response = self.client().delete(
            '/api/companies/100/delete',
            headers={'Authorization': 'Bearer ' + JWT_TOKEN})
        data = json.loads(response.data)

        self.assertEqual(response.status_code, 404)
        self.assertEqual(data['success'], False)

    # Delete without token
    def test_delete_company_without_token(self):
        response = self.client().delete('/api/companies/1/delete')
        data = json.loads(response.data)

        self.assertEqual(response.status_code, 401)
        self.assertEqual(data['success'], False)