def test_get_customer_by_username(self): """ Get customer by username """ customer = Customer.find_by_username('jf')[0] resp = self.app.get('/customers?username=jf') data = json.loads(resp.data) self.assertEqual(data[0]['username'], customer.username) self.assertEqual(data[0]['id'], customer.id) self.assertEqual(resp.status_code, status.HTTP_200_OK)
def test_get_customer(self): """ Get a single Customer """ # get the id of a customer customer = Customer.find_by_username('jf')[0] resp = self.app.get('/customers/{}'.format(customer.id), content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_200_OK) data = json.loads(resp.data) self.assertEqual(data['username'], customer.username)
def test_delete_customer(self): """ Delete a Customer """ customer = Customer.find_by_username('jf')[0] # save the current number of customers for later comparrison customer_count = self.get_customer_count() resp = self.app.delete('/customers/{}'.format(customer.id), content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(len(resp.data), 0) new_count = self.get_customer_count() self.assertEqual(new_count, customer_count - 1)
def test_deactivate_customer(self): """ Deactivate an existing Customer""" customer = Customer.find_by_username('jf')[0] new_customer = {"username": customer.username, "password": customer.password, "firstname": customer.firstname, "lastname": customer.lastname, "address": customer.address, "phone": customer.phone, "email": customer.email, "active": False, "promo": customer.promo} data = json.dumps(new_customer) resp = self.app.put('/customers/{}/deactivate'.format(customer.id), data=data, content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_200_OK) new_json = json.loads(resp.data) self.assertEqual(new_json['active'], False)
def test_update_customer(self): """ Update an existing Customer """ customer = Customer.find_by_username('jf')[0] new_customer = {"username": "******", "password": "******", "firstname": "jinfan", "lastname": "yang", "address": "nyu", "phone": "123-456-7890", "email": "*****@*****.**", "active": False, "promo": False} data = json.dumps(new_customer) resp = self.app.put('/customers/{}'.format(customer.id), data=data, content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_200_OK) new_json = json.loads(resp.data) self.assertEqual(new_json['active'], False)
def list_customers(): # yml files start with '---' # definitions can only define once, then you can use $ref to use it """ Retrieve a list of Customers This endpoint will return all Customers unless a query parameter is specificed --- tags: - Customers description: The Customers endpoint allows you to query Customers parameters: - name: username in: query description: the username of Customer you are looking for required: false type: string - name: firstname in: query description: the firstname of Customer you are looking for required: false type: string - name: lastname in: query description: the lastname of Customer you are looking for required: false type: string - name: email in: query description: the email of Customer you are looking for required: false type: string - name: active in: query description: the active status of Customer you are looking for required: false type: string - name: promo in: query description: the promotion status of Customer you are looking for required: false type: string definitions: Customer: type: object properties: id: type: integer description: unique id assigned internallt by service username: type: string description: the customer's username password: type: string description: the customer's password firstname: type: string description: the customer's firstname lastname: type: string description: the customer's lastname address: type: string description: the customer's address phone: type: string description: the customer's phone number email: type: string description: the customer's email active: type: boolean description: the active status of a customer promo: type: boolean description: the promotion status of a customer responses: 200: description: An array of Customers schema: type: array items: schema: $ref: '#/definitions/Pet' """ customers = [] username = request.args.get('username') email = request.args.get('email') firstname = request.args.get('firstname') lastname = request.args.get('lastname') promos = request.args.get('promo') actives = request.args.get('active') if username: customers = Customer.find_by_username(username) if not customers: raise NotFound( "Customer with username '{}' was not found.".format(username)) elif email: customers = Customer.find_by_email(email) if not customers: raise NotFound( "Customer with email '{}' was not found.".format(email)) elif firstname: customers = Customer.find_by_firstname(firstname) if not customers: raise NotFound( "Customer with firstname '{}' was not found.".format( firstname)) elif lastname: customers = Customer.find_by_lastname(lastname) if not customers: raise NotFound( "Customer with lastname '{}' was not found.".format(lastname)) elif promos: promos = promos.lower() if promos == 'true': promo = True elif promos == 'false': promo = False else: raise ValueError customers = Customer.find_by_promo(promo) if not customers: raise NotFound( "Customer with promotion status '{}' was not found.".format( promo)) elif actives: actives = actives.lower() if actives == 'true': active = True elif actives == 'false': active = False else: raise ValueError customers = Customer.find_by_active(active) if not customers: raise NotFound( "Customer with active status '{}' was not found.".format( active)) else: customers = Customer.all() results = [customer.serialize() for customer in customers] return make_response(jsonify(results), status.HTTP_200_OK)