Skip to content

Commit

Permalink
Merge pull request #43 from CSCI-GA-2820-SP24-003/feat-deactivate-acc…
Browse files Browse the repository at this point in the history
…ount

Feat: Deactivate account
  • Loading branch information
DIVYANSHI-PARASHAR authored Apr 2, 2024
2 parents 0836781 + 1bdb9fb commit 04ee431
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ GET /: Root URL response.
| `DELETE` | `/customers/<customer_id>` | Delete a customer with the given `id`. |
| `GET` | `/customers` | List all customers. |
| `PUT` | `/customers/<customer_id>` | Update an existing customer with the given `id`. |
| `PUT` | `/customers/<customer_id>/deactivate` | Deactivate a customer with the given `id`. |

### Usage

Expand Down
5 changes: 5 additions & 0 deletions service/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ def deserialize(self, data):
) from error
return self

def deactivate(self):
"""Deactivates the customer account"""
self.active = False
self.update()

##################################################
# CLASS METHODS
##################################################
Expand Down
53 changes: 38 additions & 15 deletions service/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


def encrypt_password(password):
""" Hashing Passwords """
"""Hashing Passwords"""
return hashlib.sha256(password.encode("UTF-8")).hexdigest()


Expand All @@ -42,17 +42,22 @@ def index():
# "Reminder: return some useful information in json format about the service here",
# status.HTTP_200_OK,
# )
return jsonify({
"1_Customers": "Welcome to the Customer Store Service API. This API allows you to manage the customers.",
"2_methods_available": {
"2.1 GET /customers/<customer_id>": "Retrieve a single customer by ID.",
"2.2 POST /customers": "Create a new customer.",
"2.3 DELETE /customers/<customer_id>": "Delete a customer by ID.",
"2.4 GET /customers": "Retrieve a list of all customers.",
"2.5 PUT /customers/<customer_id>": "Update an existing customer by ID."
},
"3_contact": "For more information, refer to the API documentation or contact [email protected]."
}), status.HTTP_200_OK
return (
jsonify(
{
"1_Customers": "Welcome to the Customer Store Service API. This API allows you to manage the customers.",
"2_methods_available": {
"2.1 GET /customers/<customer_id>": "Retrieve a single customer by ID.",
"2.2 POST /customers": "Create a new customer.",
"2.3 DELETE /customers/<customer_id>": "Delete a customer by ID.",
"2.4 GET /customers": "Retrieve a list of all customers.",
"2.5 PUT /customers/<customer_id>": "Update an existing customer by ID.",
},
"3_contact": "For more information, refer to the API documentation or contact [email protected].",
}
),
status.HTTP_200_OK,
)


######################################################################
Expand All @@ -79,9 +84,7 @@ def get_customers(customer_id):
f"Customer with id '{customer_id}' was not found.",
)

app.logger.info(
"Returning customer: %s", customer.id
)
app.logger.info("Returning customer: %s", customer.id)
return jsonify(customer.serialize()), status.HTTP_200_OK


Expand Down Expand Up @@ -175,6 +178,26 @@ def update_customers(customer_id):
return jsonify(customer.serialize()), status.HTTP_200_OK


######################################################################
# DEACTIVATE A CUSTOMER
######################################################################
@app.route("/customers/<int:customer_id>/deactivate", methods=["PUT"])
def deactivate_customer(customer_id):
"""
Deactivate a customer
This endpoint will deactivate a customer based on the id specified in the path
"""
customer = Customer.find(customer_id)
if not customer:
abort(
status.HTTP_404_NOT_FOUND,
f"Customer with id '{customer_id}' was not found.",
)

customer.deactivate()
return jsonify(""), status.HTTP_204_NO_CONTENT


######################################################################
# U T I L I T Y F U N C T I O N S
######################################################################
Expand Down
8 changes: 8 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,14 @@ def test_representing_a_customer(self):
f"<Customer {test_customer.first_name, test_customer.last_name} id=[{test_customer.id}]>",
)

def test_deactivate_a_customer(self):
"""It should Deactivate a Customer"""
customer = CustomerFactory()
customer.active = True
self.assertTrue(customer.active)
customer.deactivate()
self.assertFalse(customer.active)


######################################################################
# Q U E R Y T E S T C A S E S
Expand Down
24 changes: 24 additions & 0 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,27 @@ def test_mediatype_not_supported(self):
response = self.client.post("/customers", data="{}", content_type="text/plain")
self.assertEqual(response.status_code, 415)
self.assertIn("Unsupported media type", response.json["error"])

def test_deactivate_customer(self):
"""It should Deactivate a Customer"""
test_customer_data = CustomerFactory().serialize()
test_customer_data["active"] = True
# Create customer via POST request
create_response = self.client.post(BASE_URL, json=test_customer_data)
self.assertEqual(create_response.status_code, status.HTTP_201_CREATED)
created_customer = create_response.get_json()

# Deactivate the customer via PUT request
response = self.client.put(f"{BASE_URL}/{created_customer['id']}/deactivate")
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

# Fetch the customer to verify it's deactivated
get_response = self.client.get(f"{BASE_URL}/{created_customer['id']}")
deactivated_customer = get_response.get_json()
self.assertFalse(deactivated_customer["active"])

def test_deactivate_nonexistent_customer(self):
"""It should return 404 for a nonexistent customer"""
nonexistent_customer_id = 99999 # Assuming this ID does not exist
response = self.client.put(f"{BASE_URL}/{nonexistent_customer_id}/deactivate")
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

0 comments on commit 04ee431

Please sign in to comment.