def test_get_all_projects(self): user = User.create(email="*****@*****.**", user_type='super_admin') user.put() org = Organization.create(name="Org Foo") org.put() project1 = Project.create(organization_id=org.uid, program_label='demo-program') project2 = Project.create(organization_id=org.uid, program_label='demo-program') project1.put() project2.put() query = ''' query GetAllProjects { projects { uid } } ''' response = self.testapp.post_json( '/api/graphql', {'query': query}, headers=login_headers(user.uid), ) received = json.loads(response.body) # No particular order. self.assertIn({'uid': project1.uid}, received['projects']) self.assertIn({'uid': project2.uid}, received['projects'])
def create(): """gets data from form and add it to Users table""" if request.form: po = Project( request.form.get("name"), request.form.get("email"), request.form.get("password"), ) po.create() return redirect(url_for('project.project'))
def post(self): api_key = request.args.get('api_key') user = User() user.api_login(api_key) if not user.is_authenticated(): return jsonify({'status':False}) data = request.json project = Project() project.create(data['name'],data['description']) user.add_project(str(project.get_id())) return jsonify({'status':True})
def test_user_changes_project_task_with_account_manager(self): """Account manager notified.""" acct_mgr = User.create(email='*****@*****.**', user_type='program_admin', owned_programs=['demo-program']) other_prog = User.create(email='*****@*****.**', user_type='program_admin', owned_programs=['demo-program']) acct_mgr.put() other_prog.put() org = Organization.create(name='Foo Org') org.put() admin = User.create(email='*****@*****.**', user_type='user', name='Addi Admin') project = Project.create(program_label='demo-program', organization_id=org.uid, account_manager_id=acct_mgr.uid) task = project.tasklist.tasks[0] notifier.changed_project_task(admin, project, task) # Each account manager gets a notification, other related program # admins don't. self.assertEqual(len(acct_mgr.notifications()), 1) self.assertEqual(len(other_prog.notifications()), 0)
def test_program_admin_changes_project_task_with_liaison(self): """Liaison notified.""" org = Organization.create(name='Foo Org') admin1 = User.create(email='*****@*****.**', user_type='user', owned_organizations=[org.uid]) admin2 = User.create(email='*****@*****.**', user_type='user', owned_organizations=[org.uid]) admin1.put() admin2.put() task = org.tasklist.tasks[0] prog = User.create(email='*****@*****.**', user_type='program_admin', name='Petrarch Prog') project = Project.create(program_label='demo-program', organization_id=org.uid, liaison_id=admin1.uid) notifier.changed_project_task(prog, project, task) # Each related org admin should get a notification. self.assertEqual(len(admin1.notifications()), 1) self.assertEqual(len(admin2.notifications()), 0)
def create_project_task(self, cohort_date=datetime.datetime.today()): program_label = 'demo-program' task_label = 'task_project_foo' project = Project.create(program_label=program_label, organization_id='Organization_Foo') task_template = { 'label': task_label, 'data_type': 'radio', 'select_options': [{ 'value': 'normal', 'label': 'true names' }, { 'value': 'alias', 'label': 'aliases' }], } Program.mock_program_config( program_label, {'project_tasklist_template': [{ 'tasks': [task_template] }]}) t = Task.create(task_label, 1, 'checkpoint_foo', parent=project, program_label=program_label) t.put() return t
def test_project_task_body(self): """Tasks should get dynamic properties from their definition.""" program_label = 'demo-program' task_label = 'task_foo' project = Project.create(program_label=program_label, organization_id='Organization_Foo') task_template = { 'label': task_label, 'body': "<p>Demo body.</p>", } Program.mock_program_config( program_label, {'project_tasklist_template': [{ 'tasks': [task_template] }]}) t = Task.create(task_label, 1, 'checkpoint_foo', parent=project, program_label=program_label) t_dict = t.to_client_dict() self.assertIsInstance(t_dict['body'], basestring) self.assertGreater(len(t_dict['body']), 0)
def test_create_select(self): program_label = 'demo-program' task_label = 'task_foo' project = Project.create(program_label=program_label, organization_id='Organization_Foo') task_template = { 'label': task_label, 'data_type': 'radio', 'select_options': [{ 'value': 'normal', 'label': 'true names' }, { 'value': 'alias', 'label': 'aliases' }], } Program.mock_program_config( program_label, {'project_tasklist_template': [{ 'tasks': [task_template] }]}) t = Task.create(task_label, 1, 'checkpoint_foo', parent=project, program_label=program_label) t_dict = t.to_client_dict() self.assertEqual(t_dict['select_options'], task_template['select_options'])
def create_project_cohort(self, cohort_date=datetime.datetime.today()): program_label = 'demo-program' cohort_label = 'demo-cohort' program = Program.get_config(program_label) org_id = 'Org_Foo' liaison_id = 'User_liaison' project = Project.create(organization_id=org_id, program_label=program_label) project.put() one_day = datetime.timedelta(days=1) cohort_config = { 'label': cohort_label, 'name': 'Demo Cohort', 'open_date': str(cohort_date - one_day), # yesterday 'close_date': str(cohort_date + one_day), # tomorrow } program['cohorts'][cohort_label] = cohort_config Program.mock_program_config( program_label, {'cohorts': { cohort_label: cohort_config }}, ) pc = ProjectCohort.create( project_id=project.uid, organization_id=org_id, program_label=program_label, cohort_label=cohort_label, liaison_id=liaison_id, ) pc.put() return pc
def test_select_checkpoints_with_offset(self): """Can use queries like "LIMIT 20,10" to get "pages" of records.""" # Create two checkpoints. program_label = 'demo-program' Program.mock_program_config( program_label, { 'project_tasklist_template': [ { 'name': 'Foo', 'label': 'checkpoint_foo', 'tasks': [] }, { 'name': 'Bar', 'label': 'checkpoint_bar', 'tasks': [] }, ] }) project = Project.create(program_label=program_label, organization_id='Organization_Foo') project.put() # Select each of the two checkpoints in different queries with one-row # pages. r1 = Checkpoint.get(program_label=program_label, n=1, offset=0) r2 = Checkpoint.get(program_label=program_label, n=1, offset=1) self.assertNotEqual(r1[0].uid, r2[0].uid)
def test_initial_values(self): """Adopt initial values if specified, overriding defaults.""" # Background program_label = 'demo-program' task_label_default = 'task_default' task_label_init = 'task_init' # One task with default values, one with a non-default initial value. task_template_default = { 'label': task_label_default, # default value of disabled is False } task_template_initial_values = { 'label': task_label_init, 'initial_values': { 'disabled': True }, # override default } Program.mock_program_config( program_label, { 'project_tasklist_template': [{ 'tasks': [ task_template_default, task_template_initial_values, ] }] }) # Creating the project will generate a tasklist with the above tasks. project = Project.create(program_label=program_label, organization_id='Organization_Foo') self.assertFalse(project.tasklist.tasks[0].disabled) self.assertTrue(project.tasklist.tasks[1].disabled)
def test_create_project(self): """All program owners notified about new projects.""" prog1 = User.create(email='*****@*****.**', user_type='program_admin', owned_programs=['demo-program']) prog2 = User.create(email='*****@*****.**', user_type='super_admin', owned_programs=['demo-program']) prog3 = User.create(email='*****@*****.**', user_type='program_admin', owned_programs=[]) prog1.put() prog2.put() prog3.put() org = Organization.create(name='Foo Org') org.put() admin = User.create(email='*****@*****.**', user_type='user', name='Addi Admin') project = Project.create(program_label='demo-program', organization_id=org.uid) notifier.created_project(admin, project) # Each super admin should get a notification. self.assertEqual(len(prog1.notifications()), 1) self.assertEqual(len(prog2.notifications()), 1) self.assertEqual(len(prog3.notifications()), 0)
def test_user_changes_project_task(self): """All program admins are notified.""" prog1 = User.create(email='*****@*****.**', user_type='program_admin', owned_programs=['demo-program']) prog2 = User.create(email='*****@*****.**', user_type='super_admin', owned_programs=['demo-program']) prog3 = User.create(email='*****@*****.**', user_type='program_admin', owned_programs=[]) prog1.put() prog2.put() prog3.put() org = Organization.create(name='Foo Org') org.put() admin = User.create(email='*****@*****.**', user_type='user', name='Addi Admin') project = Project.create(program_label='demo-program', organization_id=org.uid) task = project.tasklist.tasks[0] notifier.changed_project_task(admin, project, task) # Each related program admin should get a notification. self.assertEqual(len(prog1.notifications()), 1) self.assertEqual(len(prog2.notifications()), 1) self.assertEqual(len(prog3.notifications()), 0)
def test_get_single_project(self): user = User.create(email="*****@*****.**", user_type='super_admin') user.put() org = Organization.create(name="Org Foo") org.put() project = Project.create( organization_id=org.uid, program_label='demo-program', account_manager_id='User_001', liaison_id='User_002', priority=True, deidentification_method='total', loa_notes="Some stuff happened.", last_active=datetime.datetime.now(), ) project.put() query = ''' query GetSingleProject($uid: String!) { project(uid: $uid) { account_manager_id created deidentification_method deleted last_active liaison_id loa_notes modified organization_id organization_name organization_status priority program_description program_label program_name short_uid uid } } ''' response = self.testapp.post_json( '/api/graphql', # See http://graphql.org/learn/serving-over-http/#post-request { 'query': query, 'variables': { 'uid': project.uid }, }, headers=login_headers(user.uid), ) self.assertEqual( response.body, json.dumps({'project': project.to_client_dict()}), )
def test_checkpoint_creation(self): project = Project.create(program_label='demo-program', organization_id='Organization_Foo') project.put() checkpoints = Checkpoint.get(parent_id=project.uid) program_config = Program.get_config(project.program_label) template = program_config['project_tasklist_template'] self.assertEqual(len(checkpoints), len(template))
def test_join_org(self): """Joining an org makes task reminders for all contained tasklists.""" program_label = 'demo-program' cohort_label = 'demo-cohort' # Guarantee the dates will work by mocking the cohort config. cohort_config = { 'label': cohort_label, 'name': 'Demo Cohort', 'open_date': str(datetime.date.today()), 'close_date': str(datetime.date.today()), } Program.mock_program_config( program_label, {'cohorts': { cohort_label: cohort_config }}, ) program = Program.get_config(program_label) tasklist_template = program['surveys'][0]['survey_tasklist_template'] owner = User.create(email='*****@*****.**', user_type='user') org = Organization.create(name="Foo Org", liaison_id=owner.uid) owner.owned_organizations = [org.uid] project = Project.create(program_label=program_label, organization_id=org.uid) survey = Survey.create(tasklist_template, project_id=project.uid, organization_id=org.uid, program_label=program_label, ordinal=1, cohort_label=cohort_label) owner.put() org.put() project.put() survey.put() # The assumption here is the org and its contents are long-standing, # so force consistency with all. org.key.get() project.key.get() survey.key.get() joiner = User.create(email='*****@*****.**', user_type='user') joiner.put() self.testapp.post_json( '/api/users/{}/organizations'.format(joiner.uid), org.to_client_dict(), headers=login_headers(owner.uid), ) # One TaskReminder for each of: org tasklist, project tasklist, survey # tasklist. self.assertEqual(len(TaskReminder.get(ancestor=joiner)), 3) return (org, project, survey, owner, joiner)
def test_update_checkpoint(self): project = Project.create(program_label='demo-program', organization_id='Organization_Foo') project.put() checkpoint = Checkpoint.get(parent_id=project.uid)[0] checkpoint.name = 'foo' checkpoint.put() fetched = Checkpoint.get_by_id(checkpoint.uid) self.assertEqual(fetched.name, 'foo')
def test_organization_name(self): """Updating a project should update organization name""" organization = Organization.create(name='Foo College') organization.put() project = Project.create(program_label='demo-program', organization_id=organization.uid) project.put() self.assertEqual(project.to_client_dict()['organization_name'], organization.name)
def test_task_creation(self): project = Project.create(program_label='demo-program', organization_id='Organization_Foo') project.put() tasks = Task.get(ancestor=project) program_config = Program.get_config(project.program_label) template = program_config['project_tasklist_template'] num_tasks = sum([len(checkpoint['tasks']) for checkpoint in template]) self.assertEqual(len(tasks), num_tasks)
def test_program_access(self): """Should be able to look up program config through project.""" organization = Organization.create(name='Foo College') organization.put() project = Project.create(program_label='demo-program', organization_id=organization.uid) project.put() p_dict = project.to_client_dict() self.assertEqual(p_dict['program_name'], 'Demo Program') self.assertIsInstance(p_dict['program_description'], basestring)
def test_queue_org_welcome(self): Program.mock_program_config('p1', {'project_tasklist_template': []}) Program.mock_program_config('p2', {'project_tasklist_template': []}) old_project = Project.create( program_label='p1', organization_id='Organization_foo', created=datetime.datetime.now() - datetime.timedelta(hours=48), ) old_project.put() new_project = Project.create( program_label='p1', organization_id='Organization_foo', ) new_project.put() other_project = Project.create( program_label='p2', # no matching template organization_id='Organization_foo', ) other_project.put() templates = [ self.create_mandrill_template('p1-{}'.format( auto_prompt.ORG_WELCOME_SUFFIX)), self.create_mandrill_template('foo-template'), ] auto_prompt.queue_org_welcome(templates) tasks = self.taskqueue_stub.get_filtered_tasks() # Only the recently created project, which also has a org welcome # template, should be queued. The old project, and the project on the # other program, should not be welcomed. self.assertEqual(len(tasks), 1) expected_url = '/task/email_project/{}/p1-org-welcome'.format( new_project.uid) self.assertIn(expected_url, [t.url for t in tasks]) Program.reset_mocks()
def test_organization_name_update(self): """Updating an organization name should update associated project""" org_name_1 = 'Foo College' org_name_2 = 'Bar College' organization = Organization.create(name=org_name_1) organization.put() project = Project.create(program_label='demo-program', organization_id=organization.uid) project.put() organization = Organization.get_by_id(organization.uid) organization.name = org_name_2 organization.put() project_dict = project.get_by_id(project.uid).to_client_dict() self.assertEqual(project_dict['organization_name'], organization.name)
def create_with_pc(self): project = Project.create( program_label='demo-program', organization_id='Organization_Foo', ) project.put() pc = ProjectCohort.create( program_label=project.program_label, organization_id=project.organization_id, project_id=project.uid, cohort_label='2018', ) pc.put() return project.key.get(), pc.key.get()
def create_with_project_cohort(self): checkpoint_template = { 'name': "Survey Foo", 'label': 'demo_survey__foo', 'body': "foo", 'tasks': [], } config = { 'surveys': [ { 'name': "Student Module", 'survey_tasklist_template': [checkpoint_template], }, ], } Program.mock_program_config(self.program_label, config) org = Organization.create(name='Foo Org') org.put() project = Project.create( program_label=self.program_label, organization_id=org.uid, ) project.put() pc = ProjectCohort.create( program_label=self.program_label, organization_id=org.uid, project_id=project.uid, ) pc.put() checkpoint = Checkpoint.create(parent_id='Survey_foo', ordinal=1, program_label=self.program_label, organization_id=org.uid, project_id=project.uid, project_cohort_id=pc.uid, status='incomplete', **checkpoint_template) checkpoint.put() pc.update_cached_properties() return org, project, pc, checkpoint
def test_join_cohort(self, cohort_date=datetime.date.today()): """Allowed for org admin owner of project.""" # Existing things to relate to. program_label = 'demo-program' cohort_label = 'demo-cohort' program = Program.get_config(program_label) org_id = 'Org_Foo' user = User.create(email="*****@*****.**", owned_organizations=[org_id]) project = Project.create(organization_id=org_id, program_label=program_label) user.put() project.put() # Guarantee the dates will work by mocking the cohort config. one_day = datetime.timedelta(days=1) cohort_config = { 'label': cohort_label, 'name': 'Demo Cohort', 'open_date': str(cohort_date - one_day), # yesterday 'close_date': str(cohort_date + one_day), # tomorrow } program['cohorts'][cohort_label] = cohort_config Program.mock_program_config( program_label, {'cohorts': {cohort_label: cohort_config}}, ) # Create the project cohort through the api. Any response other than # 200 will fail the test. response = self.testapp.post_json( '/api/project_cohorts', { 'project_id': project.uid, 'organization_id': org_id, 'program_label': program_label, 'cohort_label': cohort_label, 'liaison_id': user.uid, }, headers=login_headers(user.uid) ) response_dict = json.loads(response.body) return (ProjectCohort.get_by_id(response_dict['uid']), user)
def create_org_with_pc(self): org = Organization.create(name="Foo College") org.put() project = Project.create( program_label='demo-program', organization_id=org.uid, ) project.put() pc = ProjectCohort.create( program_label=project.program_label, organization_id=org.uid, project_id=project.uid, cohort_label='2018', ) pc.put() # Simulate consistency return org, project.key.get(), pc.key.get()
def test_complete_project_tasklist(self): """Liaisons and account managers notified re complete tasklist.""" org = Organization.create(name='Foo Org') project = Project.create(program_label='demo-program', organization_id=org.uid) prog = User.create(email='*****@*****.**', user_type='prog_admin') admin = User.create(email='*****@*****.**', user_type='user', owned_organizations=[org.uid]) prog.put() admin.put() org.liaison_id = admin.uid project.account_manager_id = prog.uid notifier.completed_task_list(prog, project) self.assertEqual(len(prog.notifications()), 1) self.assertEqual(len(admin.notifications()), 1)
def create_project_cohort(self, cohort_date=datetime.datetime.today()): program = Program.get_config(self.program_label) liaison = User.create(email='*****@*****.**') org = Organization.create(name="Org Foo", liaison_id=liaison.uid) liaison.owned_organizations.append(org.uid) project = Project.create(organization_id=org.uid, program_label=self.program_label) liaison.put() org.put() project.put() pc = ProjectCohort.create( project_id=project.uid, organization_id=org.uid, program_label=self.program_label, cohort_label=self.cohort_label, liaison_id=liaison.uid, ) pc.put() surveys = Survey.create_for_project_cohort(program['surveys'], pc) ndb.put_multi(surveys) return liaison, org, project, pc, surveys
def test_get_checkpoint_by_id(self): project = Project.create(program_label='demo-program', organization_id='Organization_Foo') project.put() checkpoints = Checkpoint.get(parent_id=project.uid) self.assertIsNotNone(Checkpoint.get_by_id(checkpoints[0].uid))
def test_program_label_validation(self): """Should be impossible to create projects for invalid programs.""" with self.assertRaises(Exception): Project.create(program_label='does-not-exist', organization_id='Organization_Foo')