mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-15 07:18:58 -04:00
Tests added for dao.
This commit is contained in:
@@ -7,7 +7,7 @@ from flask.ext.sqlalchemy import SQLAlchemy
|
||||
from flask_login import LoginManager
|
||||
from flask_wtf import CsrfProtect
|
||||
from werkzeug.exceptions import abort
|
||||
|
||||
from app.notify_client.api_client import NotificationsAdminAPIClient
|
||||
from app.its_dangerous_session import ItsdangerousSessionInterface
|
||||
import app.proxy_fix
|
||||
from config import configs
|
||||
@@ -17,6 +17,8 @@ db = SQLAlchemy()
|
||||
login_manager = LoginManager()
|
||||
csrf = CsrfProtect()
|
||||
|
||||
notifications_api_client = NotificationsAdminAPIClient()
|
||||
|
||||
|
||||
def create_app(config_name, config_overrides=None):
|
||||
application = Flask(__name__)
|
||||
@@ -28,6 +30,8 @@ def create_app(config_name, config_overrides=None):
|
||||
init_csrf(application)
|
||||
logging.init_app(application)
|
||||
|
||||
notifications_api_client.init_app(application)
|
||||
|
||||
login_manager.init_app(application)
|
||||
login_manager.login_view = 'main.sign_in'
|
||||
|
||||
|
||||
@@ -1,47 +1,58 @@
|
||||
from datetime import datetime
|
||||
|
||||
from client.errors import HTTPError, InvalidResponse
|
||||
from sqlalchemy.orm import load_only
|
||||
|
||||
from app import db
|
||||
from app.models import Service
|
||||
from flask.ext.login import current_user
|
||||
from app import (db, notifications_api_client)
|
||||
|
||||
|
||||
def insert_new_service(service_name, user):
|
||||
service = Service(name=service_name,
|
||||
created_at=datetime.now(),
|
||||
limit=1000,
|
||||
active=False,
|
||||
restricted=True)
|
||||
add_service(service)
|
||||
service.users.append(user)
|
||||
db.session.commit()
|
||||
return service.id
|
||||
def insert_new_service(service_name, user_id):
|
||||
# Add a service with default attributes
|
||||
# Should we try and handle exception here
|
||||
resp = notifications_api_client.create_service(
|
||||
service_name, False, 1000, True, user_id)
|
||||
|
||||
return resp['data']['id']
|
||||
|
||||
|
||||
def get_service_by_id(id):
|
||||
return Service.query.get(id)
|
||||
def get_service_by_id(id_):
|
||||
return notifications_api_client.get_service(id_)
|
||||
|
||||
|
||||
def unrestrict_service(service_id):
|
||||
service = get_service_by_id(service_id)
|
||||
service.restricted = False
|
||||
add_service(service)
|
||||
resp = notifications_api_client.get_service(service_id)
|
||||
if resp['data']['restricted']:
|
||||
resp = notifications_api_client.update_service(
|
||||
service_id,
|
||||
resp['data']['name'],
|
||||
resp['data']['active'],
|
||||
resp['data']['limit'],
|
||||
False,
|
||||
resp['data']['users'])
|
||||
|
||||
|
||||
def activate_service(service_id):
|
||||
service = get_service_by_id(service_id)
|
||||
service.active = True
|
||||
add_service(service)
|
||||
|
||||
|
||||
def add_service(service):
|
||||
db.session.add(service)
|
||||
db.session.commit()
|
||||
resp = notifications_api_client.get_service(service_id)
|
||||
if not resp['data']['active']:
|
||||
resp = notifications_api_client.update_service(
|
||||
service_id,
|
||||
resp['data']['name'],
|
||||
True,
|
||||
resp['data']['limit'],
|
||||
resp['data']['restricted'],
|
||||
resp['data']['users'])
|
||||
|
||||
|
||||
# TODO Fix when functionality is added to the api.
|
||||
def find_service_by_service_name(service_name):
|
||||
return Service.query.filter_by(name=service_name).first()
|
||||
resp = notifications_api_client.get_services()
|
||||
retval = None
|
||||
for srv_json in resp['data']:
|
||||
if srv_json['name'] == service_name:
|
||||
retval = srv_json
|
||||
break
|
||||
return retval
|
||||
|
||||
|
||||
def find_all_service_names():
|
||||
return [x.name for x in Service.query.options(load_only("name")).all()]
|
||||
resp = notifications_api_client.get_services()
|
||||
return [x['name'] for x in resp['data']]
|
||||
|
||||
@@ -8,9 +8,9 @@ from app.main.forms import AddServiceForm
|
||||
@main.route("/add-service", methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def add_service():
|
||||
# TODO fix up this
|
||||
form = AddServiceForm(services_dao.find_all_service_names())
|
||||
if form.validate_on_submit():
|
||||
|
||||
user = users_dao.get_user_by_id(session['user_id'])
|
||||
services_dao.insert_new_service(form.service_name.data, user)
|
||||
return redirect(url_for('.dashboard', service_id=123))
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import datetime
|
||||
from app import db
|
||||
from flask import current_app
|
||||
|
||||
@@ -33,12 +34,21 @@ class User(db.Model):
|
||||
email_address = db.Column(db.String(255), nullable=False, index=True, unique=True)
|
||||
password = db.Column(db.String, index=False, unique=False, nullable=False)
|
||||
mobile_number = db.Column(db.String, index=False, unique=False, nullable=False)
|
||||
created_at = db.Column(db.DateTime, index=False, unique=False, nullable=False)
|
||||
updated_at = db.Column(db.DateTime, index=False, unique=False, nullable=True)
|
||||
created_at = db.Column(db.DateTime,
|
||||
index=False,
|
||||
unique=False,
|
||||
nullable=False,
|
||||
default=datetime.datetime.now)
|
||||
updated_at = db.Column(db.DateTime,
|
||||
index=False,
|
||||
unique=False,
|
||||
nullable=True,
|
||||
onupdate=datetime.datetime.now)
|
||||
password_changed_at = db.Column(db.DateTime, index=False, unique=False, nullable=True)
|
||||
role_id = db.Column(db.Integer, db.ForeignKey('roles.id'), index=True, unique=False, nullable=False)
|
||||
logged_in_at = db.Column(db.DateTime, nullable=True)
|
||||
failed_login_count = db.Column(db.Integer, nullable=False, default=0)
|
||||
# TODO should this be an enum?
|
||||
state = db.Column(db.String, nullable=False, default='pending')
|
||||
|
||||
def serialize(self):
|
||||
@@ -78,37 +88,37 @@ class User(db.Model):
|
||||
return True
|
||||
|
||||
|
||||
user_to_service = db.Table(
|
||||
'user_to_service',
|
||||
db.Model.metadata,
|
||||
db.Column('user_id', db.Integer, db.ForeignKey('users.id')),
|
||||
db.Column('service_id', db.Integer, db.ForeignKey('services.id'))
|
||||
)
|
||||
# user_to_service = db.Table(
|
||||
# 'user_to_service',
|
||||
# db.Model.metadata,
|
||||
# db.Column('user_id', db.Integer, db.ForeignKey('users.id')),
|
||||
# db.Column('service_id', db.Integer, db.ForeignKey('services.id'))
|
||||
# )
|
||||
|
||||
|
||||
class Service(db.Model):
|
||||
__tablename__ = 'services'
|
||||
# class Service(db.Model):
|
||||
# __tablename__ = 'services'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(255), nullable=False, unique=True)
|
||||
created_at = db.Column(db.DateTime, index=False, unique=False, nullable=False)
|
||||
active = db.Column(db.Boolean, index=False, unique=False, nullable=False)
|
||||
limit = db.Column(db.BigInteger, index=False, unique=False, nullable=False)
|
||||
users = db.relationship('User', secondary=user_to_service, backref=db.backref('user_to_service', lazy='dynamic'))
|
||||
restricted = db.Column(db.Boolean, index=False, unique=False, nullable=False)
|
||||
# id = db.Column(db.Integer, primary_key=True)
|
||||
# name = db.Column(db.String(255), nullable=False, unique=True)
|
||||
# created_at = db.Column(db.DateTime, index=False, unique=False, nullable=False)
|
||||
# active = db.Column(db.Boolean, index=False, unique=False, nullable=False)
|
||||
# limit = db.Column(db.BigInteger, index=False, unique=False, nullable=False)
|
||||
# users = db.relationship('User', secondary=user_to_service, backref=db.backref('user_to_service', lazy='dynamic'))
|
||||
# restricted = db.Column(db.Boolean, index=False, unique=False, nullable=False)
|
||||
|
||||
def serialize(self):
|
||||
serialized = {
|
||||
'id': self.id,
|
||||
'name': self.name,
|
||||
'createdAt': self.created_at.strftime(DATETIME_FORMAT),
|
||||
'active': self.active,
|
||||
'restricted': self.restricted,
|
||||
'limit': self.limit,
|
||||
'user': self.users.serialize()
|
||||
}
|
||||
# def serialize(self):
|
||||
# serialized = {
|
||||
# 'id': self.id,
|
||||
# 'name': self.name,
|
||||
# 'createdAt': self.created_at.strftime(DATETIME_FORMAT),
|
||||
# 'active': self.active,
|
||||
# 'restricted': self.restricted,
|
||||
# 'limit': self.limit,
|
||||
# 'user': self.users.serialize()
|
||||
# }
|
||||
|
||||
return filter_null_value_fields(serialized)
|
||||
# return filter_null_value_fields(serialized)
|
||||
|
||||
|
||||
def filter_null_value_fields(obj):
|
||||
|
||||
@@ -4,7 +4,19 @@ from client.notifications import NotificationsAPIClient
|
||||
|
||||
class NotificationsAdminAPIClient(NotificationsAPIClient):
|
||||
|
||||
def create_service(self, service_name, active, limit, restricted):
|
||||
# Fudge assert in the super __init__ so
|
||||
# we can set those variables later.
|
||||
def __init__(self):
|
||||
super(NotificationsAdminAPIClient, self).__init__("api_url",
|
||||
"client",
|
||||
"secret")
|
||||
|
||||
def init_app(self, application):
|
||||
self.base_url = application.config['NOTIFY_API_URL']
|
||||
self.client_id = application.config['NOTIFY_API_CLIENT']
|
||||
self.secret = application.config['NOTIFY_API_SECRET']
|
||||
|
||||
def create_service(self, service_name, active, limit, restricted, user_id):
|
||||
"""
|
||||
Create a service and return the json.
|
||||
"""
|
||||
@@ -12,6 +24,7 @@ class NotificationsAdminAPIClient(NotificationsAPIClient):
|
||||
"name": service_name,
|
||||
"active": active,
|
||||
"limit": limit,
|
||||
"users": [user_id],
|
||||
"restricted": restricted
|
||||
}
|
||||
return self.post("/service", data)
|
||||
@@ -28,7 +41,8 @@ class NotificationsAdminAPIClient(NotificationsAPIClient):
|
||||
service_name,
|
||||
active,
|
||||
limit,
|
||||
restricted):
|
||||
restricted,
|
||||
users):
|
||||
"""
|
||||
Update a service.
|
||||
"""
|
||||
@@ -37,7 +51,8 @@ class NotificationsAdminAPIClient(NotificationsAPIClient):
|
||||
"name": service_name,
|
||||
"active": active,
|
||||
"limit": limit,
|
||||
"restricted": restricted
|
||||
"restricted": restricted,
|
||||
"users": users
|
||||
}
|
||||
endpoint = "/service/{0}".format(service_id)
|
||||
return self.put(endpoint, update_dict)
|
||||
@@ -61,3 +76,21 @@ class NotificationsAdminAPIClient(NotificationsAPIClient):
|
||||
"""
|
||||
endpoint = "/service/{0}/template/{1}".format(service_id, template_id)
|
||||
return self.delete(endpoint)
|
||||
|
||||
# The implementation of these will change after the notifications-api
|
||||
# functionality updates to include the ability to send notifications.
|
||||
def send_sms(self,
|
||||
mobile_number,
|
||||
message,
|
||||
job_id=None,
|
||||
description=None):
|
||||
pass
|
||||
|
||||
def send_email(self,
|
||||
email_address,
|
||||
message,
|
||||
from_address,
|
||||
subject,
|
||||
job_id=None,
|
||||
description=None):
|
||||
pass
|
||||
|
||||
@@ -2,6 +2,7 @@ from random import randint
|
||||
from flask import url_for, current_app
|
||||
from itsdangerous import URLSafeTimedSerializer, SignatureExpired
|
||||
from app.main.dao import verify_codes_dao
|
||||
from app import notifications_api_client
|
||||
|
||||
|
||||
def create_verify_code():
|
||||
@@ -11,7 +12,8 @@ def create_verify_code():
|
||||
def send_sms_code(user_id, mobile_number):
|
||||
sms_code = create_verify_code()
|
||||
verify_codes_dao.add_code(user_id=user_id, code=sms_code, code_type='sms')
|
||||
# admin_api_client.send_sms(mobile_number=mobile_number, message=sms_code, token=admin_api_client.auth_token)
|
||||
notifications_api_client.send_sms(mobile_number=mobile_number,
|
||||
message=sms_code)
|
||||
|
||||
return sms_code
|
||||
|
||||
@@ -19,21 +21,19 @@ def send_sms_code(user_id, mobile_number):
|
||||
def send_email_code(user_id, email):
|
||||
email_code = create_verify_code()
|
||||
verify_codes_dao.add_code(user_id=user_id, code=email_code, code_type='email')
|
||||
# admin_api_client.send_email(email_address=email,
|
||||
# from_str='notify@digital.cabinet-office.gov.uk',
|
||||
# message=email_code,
|
||||
# subject='Verification code',
|
||||
# token=admin_api_client.auth_token)
|
||||
notifications_api_client.send_email(email_address=email,
|
||||
from_address='notify@digital.cabinet-office.gov.uk',
|
||||
message=email_code,
|
||||
subject='Verification code')
|
||||
return email_code
|
||||
|
||||
|
||||
def send_change_password_email(email):
|
||||
link_to_change_password = url_for('.new_password', token=generate_token(email), _external=True)
|
||||
# admin_api_client.send_email(email_address=email,
|
||||
# from_str='notify@digital.cabinet-office.gov.uk',
|
||||
# message=link_to_change_password,
|
||||
# subject='Reset password for GOV.UK Notify',
|
||||
# token=admin_api_client.auth_token)
|
||||
notifications_api_client.send_email(email_address=email,
|
||||
from_address='notify@digital.cabinet-office.gov.uk',
|
||||
message=link_to_change_password,
|
||||
subject='Reset password for GOV.UK Notify')
|
||||
|
||||
|
||||
def generate_token(email):
|
||||
|
||||
Reference in New Issue
Block a user