Ejemplo n.º 1
0
def logged_out_user_with_email_and_password(context, state, email, password):
    context.user = UserFactory(
        email=email,
        password=password,
        state=state,
    )
    DjangoClient().logout()
Ejemplo n.º 2
0
 def test_grids_list_get(self):
     iris = load_iris()
     client = DjangoClient()
     response = client.get(reverse('grids_list'))
     self.assertEqual(200, response.status_code)
     self.assertEqual(0, len(response.data))
     gs1 = ATGridSearchCV(tree.DecisionTreeClassifier(), {
         'criterion': ['gini', 'entropy'],
         'max_depth': range(1, 6),
         'max_features': ['auto', 'log2']
     },
                          webserver_url=self.live_server_url)
     wait(gs1.fit(iris.data, iris.target))
     response = client.get(reverse('grids_list'))
     self.assertEqual(200, response.status_code)
     self.assertEqual(1, len(response.data))
     gs2 = ATGridSearchCV(tree.ExtraTreeClassifier(), {
         'criterion': ['gini', 'entropy'],
         'max_depth': range(1, 6),
         'max_features': ['auto', 'log2']
     },
                          webserver_url=self.live_server_url)
     wait(gs2.fit(iris.data, iris.target))
     response = client.get(reverse('grids_list'))
     self.assertEqual(200, response.status_code)
     self.assertEqual(2, len(response.data))
Ejemplo n.º 3
0
def logged_user_type(context, user_type):
    _factory = factories_registry.get_factory(user_type)
    context.user = _factory(
        email='{}@dane.gov.pl'.format(user_type.replace(' ', '_')),
        password='******',
    )
    DjangoClient().force_login(context.user)
Ejemplo n.º 4
0
    def test_dataset_grids_get(self):
        reg = linear_model.LinearRegression()
        examples_file, label_file = _create_dataset()
        ds, _ = DataSet.objects.get_or_create(
            name='TEST',
            examples=SimpleUploadedFile(examples_file.name,
                                        examples_file.read()),
            labels=SimpleUploadedFile(label_file.name, label_file.read()))

        gs_, _ = GridSearch.objects.get_or_create(
            classifier=reg.__class__.__name__, dataset=ds)
        client = DjangoClient()
        response = client.get(reverse('dataset_grids', kwargs={'name':
                                                               'TEST'}))
        self.assertEqual(200, response.status_code)
        self.assertEqual(1, len(response.data))
        gs_1 = ATGridSearchCV(ensemble.RandomForestClassifier(), {
            'criterion': ['gini', 'entropy'],
            'max_depth': range(1, 21),
            'max_features': ['auto', 'log2', 'sqrt', None]
        },
                              dataset=ds.pk,
                              webserver_url=self.live_server_url)
        wait(gs_1.fit())
        response = client.get(reverse('dataset_grids', kwargs={'name':
                                                               'TEST'}))
        self.assertEqual(200, response.status_code)
        self.assertEqual(2, len(response.data))
Ejemplo n.º 5
0
    def test_atgridsearch_post(self):
        examples_file, label_file = _create_dataset()
        ds, _ = DataSet.objects.get_or_create(
            name='TEST',
            examples=SimpleUploadedFile(examples_file.name,
                                        examples_file.read()),
            labels=SimpleUploadedFile(label_file.name, label_file.read()))
        post_data = {
            'clf': tree.DecisionTreeClassifier.__name__,
            'dataset': ds.name
        }
        post_data['args'] = {
            'criterion': 'gini, entropy',
            'max_features': {
                'start': 5,
                'end': 10,
                'skip': 1
            },
            'presort': ['True', 'False']
        }

        response = DjangoClient().post(reverse('gridsearch_create'),
                                       json.dumps(post_data),
                                       content_type="application/json")
        self.assertEqual(201, response.status_code, response.data)
Ejemplo n.º 6
0
 def test_estimators_detail(self):
     client = DjangoClient()
     for clf_name, clf in all_estimators():
         response = client.get(
             reverse('estimator_detail', kwargs={'clf': clf_name}))
         self.assertEqual(200, response.status_code)
         for param_data in response.data:
             self.assertEqual(len(param_data), 3)
Ejemplo n.º 7
0
def logged_active_user_with_newsletter_subscription_enabled(
        context, email, activation_code):
    user = UserFactory(email=email, password='******', state='active')
    subscription = Subscription.subscribe(email, user=user)
    subscription.activation_code = activation_code
    subscription.confirm_subscription()
    context.user = user
    DjangoClient().force_login(context.user)
Ejemplo n.º 8
0
 def test_estimator_detail(self):
     client = DjangoClient()
     response = client.get(
         reverse('estimator_detail',
                 kwargs={'clf': 'DecisionTreeClassifier'}))
     self.assertEqual(200, response.status_code)
     self.assertEqual(len(response.data),
                      len(tree.DecisionTreeClassifier().get_params()))
Ejemplo n.º 9
0
    def start_django_client(self, username, password):

        # start django client
        django_client = DjangoClient()

        # login as admin
        django_client.login(username=username, password=password)

        return django_client
Ejemplo n.º 10
0
def logged_out_agent_user_created_with_params(context, params):
    _factory = factories_registry.get_factory('agent user')
    kwargs = {
        'email': '*****@*****.**',
        'password': '******',
    }
    kwargs.update(json.loads(params))
    context.user = _factory(**kwargs)
    DjangoClient().logout()
Ejemplo n.º 11
0
def generic_user_with_data(object_type, user_data, context, admin_context):
    assert object_type.endswith(
        ' user'), 'this keyword is only suited for creating users'
    factory_ = factories_registry.get_factory(object_type)
    if factory_ is None:
        raise ValueError('invalid object type: %s' % object_type)
    user_data = json.loads(user_data)
    user = admin_context.admin.user = context.user = factory_(**user_data)
    DjangoClient().force_login(user)
Ejemplo n.º 12
0
 def test_dataset_post_missing_labels(self):
     examples_file, label_file = _create_dataset()
     client = DjangoClient()
     response = client.post(reverse('datasets'),
                            data={
                                'dataset': 'TEST',
                                'file[0]': examples_file
                            })
     self.assertEqual(400, response.status_code)
     self.assertEqual(b'"Missing dataset files"', response.content)
Ejemplo n.º 13
0
    def test_register_logout_then_login(self):
        # Bring an invite key, get logged in as a new user
        PW = 'testpassword'
        invite = FirmEmailInviteFactory.create(status=FirmEmailInvite.STATUS_SENT)

        url = reverse('api:v1:firm:register-user')
        data = {
            'first_name': invite.first_name,
            'last_name': invite.last_name,
            'invite_key': invite.invite_key,
            'password': PW,
            'question_one': 'what is the first answer?',
            'question_one_answer': 'answer one',
            'question_two': 'what is the second answer?',
            'question_two_answer': 'answer two',
        }

        # Accept an invitation and create a user
        response = self.client.post(url, data)
        lookup_invite = FirmEmailInvite.objects.get(pk=invite.pk)
        invite_detail_url = reverse('api:v1:firm:invite-detail', kwargs={'invite_key': invite.invite_key} )
        me_url = reverse('api:v1:user-me')
        self.assertEqual(FirmEmailInvite.STATUS_ACCEPTED, lookup_invite.status)

        # New user must be logged in and able to see invite data
        self.assertIn('sessionid', response.cookies)
        response = self.client.get(me_url)
        self.assertEqual(response.status_code, status.HTTP_200_OK,
                         msg='/api/v1/me should be valid during invitation')

        # If a session is not logged in, return 200 with data
        self.client.logout()
        response = self.client.get(me_url)
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN,
                         msg='/api/v1/me denies unauthenticated user')

        # But this user can still log in again
        # through non-api url, user prob not using api to login
        # in this scenario
        # POST ing to the backup login url FAILS here:
        self.client = DjangoClient()  # django
        url = reverse('login')
        data = {
            'username': lookup_invite.user.email,
            'password': PW,
        }
        response = self.client.post(url, data)
        # redirects to frontend client onboarding
        self.assertRedirects(response, lookup_invite.firm_onboarding_url, fetch_redirect_response=False)
        self.assertIn('sessionid', response.cookies, msg='A user still onboarding can log in')

        response = self.client.get(me_url)
        self.assertEqual(response.status_code, status.HTTP_200_OK,
                         msg='/api/v1/me works for newly authenticated user')
Ejemplo n.º 14
0
    def test_dataset_get(self):
        examples_file, label_file = _create_dataset()
        ds, _ = DataSet.objects.get_or_create(
            name='TEST',
            examples=SimpleUploadedFile(examples_file.name,
                                        examples_file.read()),
            labels=SimpleUploadedFile(label_file.name, label_file.read()))

        client = DjangoClient()
        response = client.get(reverse('datasets'))
        self.assertEqual(200, response.status_code)
        self.assertEqual(1, len(response.data))
Ejemplo n.º 15
0
 def test_dataset_post_success(self):
     examples_file, label_file = _create_dataset()
     client = DjangoClient()
     response = client.post(reverse('datasets'),
                            data={
                                'dataset': 'TEST',
                                'file[0]': examples_file,
                                'file[1]': label_file
                            })
     self.assertEqual(201, response.status_code)
     self.assertEqual(3, len(response.data))
     self.assertEqual(1, DataSet.objects.count())
Ejemplo n.º 16
0
 def test_dataset_post_exceed_files(self):
     examples_file, label_file = _create_dataset()
     client = DjangoClient()
     response = client.post(reverse('datasets'),
                            data={
                                'dataset': 'TEST',
                                'file[0]': examples_file,
                                'file[1]': label_file,
                                'file[2]': examples_file
                            })
     self.assertEqual(400, response.status_code)
     self.assertEqual(b'"Too many files"', response.content)
Ejemplo n.º 17
0
 def test_dataset_post_labels_bad_name(self):
     examples_file, label_file = _create_dataset()
     label_file.name = 'EXAMPLES.csv'
     client = DjangoClient()
     response = client.post(reverse('datasets'),
                            data={
                                'dataset': 'TEST',
                                'file[0]': examples_file,
                                'file[1]': label_file
                            })
     self.assertEqual(400, response.status_code)
     self.assertEqual(b'"Bad name of labels file"', response.content)
Ejemplo n.º 18
0
 def test_grid_detail(self):
     iris = load_iris()
     client = DjangoClient()
     gs1 = ATGridSearchCV(tree.DecisionTreeClassifier(), {
         'criterion': ['gini', 'entropy'],
         'max_depth': range(1, 6),
         'max_features': ['auto', 'log2']
     },
                          webserver_url=self.live_server_url)
     wait(gs1.fit(iris.data, iris.target))
     response = client.get(
         reverse('grid_detail', kwargs={'uuid': gs1._uuid}))
     self.assertEqual(200, response.status_code)
     self.assertEqual(response.data['uuid'], str(gs1._uuid))
Ejemplo n.º 19
0
def logged_extra_agent_with_id_of_agent_with_id(context, extra_agent_id,
                                                agent_id):
    _agent_factory = factories_registry.get_factory('agent user')
    _active_user_factory = factories_registry.get_factory('active user')
    agent = _agent_factory(
        id=agent_id,
        email='*****@*****.**',
        password='******',
    )
    user = _active_user_factory(
        id=extra_agent_id,
        email='*****@*****.**',
        password='******',
    )
    user.extra_agent_of = agent
    user.save()
    context.user = user
    DjangoClient().force_login(context.user)
Ejemplo n.º 20
0
 def test_dataset_post_duplicate_name(self):
     examples_file, label_file = _create_dataset()
     client = DjangoClient()
     response = client.post(reverse('datasets'),
                            data={
                                'dataset': 'TEST',
                                'file[0]': examples_file,
                                'file[1]': label_file
                            })
     self.assertEqual(201, response.status_code)
     response = client.post(reverse('datasets'),
                            data={
                                'dataset': 'TEST',
                                'file[0]': examples_file,
                                'file[1]': label_file
                            })
     self.assertEqual(400, response.status_code)
     self.assertEqual(b'"Name already exists"', response.content)
Ejemplo n.º 21
0
 def test_dataset_post_dataset_length_mismatch(self):
     examples_file, label_file = BytesIO(), BytesIO()
     examples_file.name = 'examples.csv'
     label_file.name = 'labels.csv'
     iris = load_iris()
     breast_cancer = load_breast_cancer()
     numpy.savetxt(examples_file, iris.data, delimiter=',')
     numpy.savetxt(label_file, breast_cancer.target, delimiter=',')
     examples_file.seek(0), label_file.seek(0)
     client = DjangoClient()
     response = client.post(reverse('datasets'),
                            data={
                                'dataset': 'TEST',
                                'file[0]': examples_file,
                                'file[1]': label_file
                            })
     self.assertEqual(400, response.status_code)
     self.assertEqual(b'"Examples and labels are not the same length"',
                      response.content)
Ejemplo n.º 22
0
    def test_atgridsearch_post_no_dataset(self):
        post_data = {
            'clf': tree.DecisionTreeClassifier.__name__,
            'dataset': 'TEST'
        }
        post_data['args'] = {
            'criterion': 'gini, entropy',
            'max_features': {
                'start': 5,
                'end': 10,
                'skip': 1
            }
        }

        response = DjangoClient().post(reverse('gridsearch_create'),
                                       json.dumps(post_data),
                                       content_type="application/json")
        self.assertEqual(400, response.status_code)
        self.assertEqual(b'"No DataSet named TEST"', response.content)
Ejemplo n.º 23
0
 def test_dataset_grid_results(self):
     examples, labels = _create_dataset()
     ds, _ = DataSet.objects.get_or_create(
         name='TEST',
         examples=SimpleUploadedFile(examples.name, examples.read()),
         labels=SimpleUploadedFile(labels.name, labels.read()))
     gs = ATGridSearchCV(tree.DecisionTreeClassifier(), {
         'criterion': ['gini', 'entropy'],
         'max_depth': range(1, 21),
         'max_features': ['auto', 'log2', 'sqrt', None]
     },
                         dataset=ds.pk,
                         webserver_url=self.live_server_url)
     wait(gs.fit())
     client = DjangoClient()
     response = client.get(
         reverse('grid_results', kwargs={'uuid': gs._uuid}))
     self.assertEqual(200, response.status_code)
     self.assertEqual(
         GridSearch.objects.get(uuid=gs._uuid).results.all().count(),
         len(response.data))
Ejemplo n.º 24
0
    def test_atgridsearch_post_no_clf(self):
        examples_file, label_file = _create_dataset()
        ds, _ = DataSet.objects.get_or_create(
            name='TEST',
            examples=SimpleUploadedFile(examples_file.name,
                                        examples_file.read()),
            labels=SimpleUploadedFile(label_file.name, label_file.read()))
        post_data = {'clf': 'Tree', 'dataset': ds.name}
        post_data['args'] = {
            'criterion': 'gini, entropy',
            'max_features': {
                'start': 5,
                'end': 10,
                'skip': 1
            }
        }

        response = DjangoClient().post(reverse('gridsearch_create'),
                                       json.dumps(post_data),
                                       content_type="application/json")
        self.assertEqual(400, response.status_code)
        self.assertEqual(b'"No sklearn classifier named Tree"',
                         response.content)
Ejemplo n.º 25
0
    def test_create_client(self):
        # We need an accepted invitation to be able to create a client
        usr = UserFactory.create()
        EmailInviteFactory.create(user=usr, status=EmailInvite.STATUS_ACCEPTED)
        url = reverse('api:v1:client-list')
        address = {
            "address": "123 My Street\nSome City",
            "post_code": "112233",
            "region": {
                "name": "New South Wales",
                "country": "AU",
                "code": "NSW",
            }
        }
        regional_data = {
            'ssn': '555-55-5555',
            'politically_exposed': True,
        }
        data = {
            "advisor_agreement": True,
            "betasmartz_agreement": True,
            "date_of_birth": date(2016, 9, 21),
            "employment_status": EMPLOYMENT_STATUS_FULL_TIME,
            "gender": GENDER_MALE,
            "income": 1234,
            "phone_num": "+1-234-234-2342",
            "residential_address": address,
            "regional_data": json.dumps(regional_data)
        }
        self.client.force_authenticate(usr)
        response = self.client.post(url, data)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        # Ensure it is created approved and accepted.
        self.assertEqual(response.data['is_confirmed'], True)
        self.assertEqual(response.data['is_accepted'], True)
        self.assertEqual(response.data['phone_num'], "+12342342342")
        self.assertEqual(response.data['residential_address']['address'],
                         address['address'])
        self.assertEqual(response.data['residential_address']['post_code'],
                         address['post_code'])
        self.assertEqual(response.data['betasmartz_agreement'], True)
        self.assertEqual(response.data['advisor_agreement'], True)
        regional_data_load = json.loads(response.data['regional_data'])
        self.assertEqual(regional_data_load['ssn'], regional_data['ssn'])
        self.assertEqual(regional_data_load['politically_exposed'],
                         regional_data['politically_exposed'])

        # check onboarding status is complete
        lookup_invite = EmailInvite.objects.get(user=usr)
        self.assertEqual(lookup_invite.status, EmailInvite.STATUS_COMPLETE)

        # can login with new client
        self.client = DjangoClient()  # django
        url = reverse('login')
        data = {
            'username': usr.email,
            'password': '******',
        }
        response = self.client.post(url, data)
        # redirect to application
        self.assertRedirects(response,
                             reverse('client:page', args=[
                                 usr.client.id,
                             ]))

        # can retrieve profile info ok
        url = reverse('api:v1:user-me')
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
Ejemplo n.º 26
0
    def test(self):

        # Start client as admin
        broker = {
            'HOST': 'localhost',
            'PORT': 1883,
            'KEEPALIVE': 60,
            'CLEAN_SESSION': True
        }

        # Start subscribe client
        broker.update(settings.MQTT)
        broker['CLIENT_ID'] = 'test_mqttclient'

        subscribe_client = SubscribeClient(broker, debug=True)
        self.is_connected(subscribe_client)
        self.is_subscribed(subscribe_client)

        # Start test client
        broker.update(settings.MQTT)
        client_id = 'test_mqtt_subscribe_admin'
        username = broker['USERNAME']
        broker['CLIENT_ID'] = client_id

        test_client = MQTTTestClient(broker, check_payload=False, debug=True)
        self.is_connected(test_client)

        # start django client
        django_client = DjangoClient()

        # login as admin
        django_client.login(username=settings.MQTT['USERNAME'],
                            password=settings.MQTT['PASSWORD'])

        # handshake ambulance and hospital
        response = django_client.post('/en/api/client/',
                                      content_type='application/json',
                                      data=json.dumps({
                                          'client_id':
                                          client_id,
                                          'status':
                                          ClientStatus.O.name,
                                          'ambulance':
                                          self.a1.id,
                                          'hospital':
                                          self.h1.id
                                      }),
                                      follow=True)
        self.assertEqual(response.status_code, 201)

        # check record
        clnt = Client.objects.get(client_id=client_id)
        self.assertEqual(clnt.status, ClientStatus.O.name)
        self.assertEqual(clnt.ambulance, self.a1)
        self.assertEqual(clnt.hospital, self.h1)

        # Modify ambulance

        # retrieve current ambulance status
        obj = Ambulance.objects.get(id=self.a1.id)
        self.assertEqual(obj.status, AmbulanceStatus.UK.name)

        # expect update once
        test_client.expect('ambulance/{}/data'.format(self.a1.id))
        self.is_subscribed(test_client)

        # publish change
        test_client.publish('user/{}/client/{}/ambulance/{}/data'.format(
            self.u1.username, client_id, self.a1.id),
                            json.dumps({
                                'status': AmbulanceStatus.OS.name,
                            }),
                            qos=0)

        # process messages
        self.loop(test_client, subscribe_client)

        # verify change
        obj = Ambulance.objects.get(id=self.a1.id)
        self.assertEqual(obj.status, AmbulanceStatus.OS.name)

        # Modify hospital

        # retrieve current hospital status
        obj = Hospital.objects.get(id=self.h1.id)
        self.assertEqual(obj.comment, 'no comments')

        # expect update once
        test_client.expect('hospital/{}/data'.format(self.h1.id))
        self.is_subscribed(test_client)

        test_client.publish('user/{}/client/{}/hospital/{}/data'.format(
            self.u1.username, client_id, self.h1.id),
                            json.dumps({
                                'comment': 'no more comments',
                            }),
                            qos=0)

        # process messages
        self.loop(test_client, subscribe_client)

        # verify change
        obj = Hospital.objects.get(id=self.h1.id)
        self.assertEqual(obj.comment, 'no more comments')

        # Modify hospital equipment

        # retrieve current equipment value
        obj = EquipmentItem.objects.get(
            equipmentholder=self.h1.equipmentholder, equipment=self.e1)
        self.assertEqual(obj.value, 'True')

        # expect update once
        test_client.expect('equipment/{}/item/{}/data'.format(
            self.h1.equipmentholder.id, self.e1.id))
        self.is_subscribed(test_client)

        test_client.publish(
            'user/{}/client/{}/equipment/{}/item/{}/data'.format(
                self.u1.username, client_id, self.h1.equipmentholder.id,
                self.e1.id),
            json.dumps({
                'value': 'False',
            }),
            qos=0)

        # process messages
        self.loop(test_client, subscribe_client)

        # verify change
        obj = EquipmentItem.objects.get(
            equipmentholder=self.h1.equipmentholder, equipment=self.e1)
        self.assertEqual(obj.value, 'False')

        # test bulk ambulance update

        # expect null client after logout
        test_client.expect('ambulance/{}/data'.format(self.a1.id))
        self.is_subscribed(test_client)

        # handshake ambulance
        response = django_client.post('/en/api/client/',
                                      content_type='application/json',
                                      data=json.dumps({
                                          'client_id':
                                          client_id,
                                          'status':
                                          ClientStatus.O.name,
                                          'ambulance':
                                          self.a2.id,
                                      }),
                                      follow=True)
        # result = JSONParser().parse(BytesIO(response.content))
        # logger.debug(result)
        self.assertEqual(response.status_code, 201)

        # check record
        clnt = Client.objects.get(client_id=client_id)
        self.assertEqual(clnt.status, ClientStatus.O.name)
        self.assertEqual(clnt.ambulance, self.a2)
        self.assertEqual(clnt.hospital, self.h1)

        # retrieve last ambulance
        obj = Ambulance.objects.get(id=self.a1.id)
        self.assertEqual(hasattr(obj, 'client'), False)

        # retrieve current ambulance status
        obj = Ambulance.objects.get(id=self.a2.id)
        self.assertEqual(obj.status, AmbulanceStatus.UK.name)

        location = {'latitude': -2., 'longitude': 7.}
        timestamp = timezone.now()
        data = [{
            'status': AmbulanceStatus.OS.name,
        }, {
            'status': AmbulanceStatus.AV.name,
            'location': location,
        }, {
            'status': AmbulanceStatus.PB.name,
            'timestamp': str(timestamp)
        }]

        # expect update once
        test_client.expect('ambulance/{}/data'.format(self.a2.id))
        self.is_subscribed(test_client)

        test_client.publish('user/{}/client/{}/ambulance/{}/data'.format(
            self.u1.username, client_id, self.a2.id),
                            json.dumps(data),
                            qos=0)

        # process messages
        self.loop(test_client, subscribe_client)

        # verify change
        obj = Ambulance.objects.get(id=self.a2.id)
        self.assertEqual(obj.status, AmbulanceStatus.PB.name)
        self.assertEqual(obj.timestamp, timestamp)
        self.assertEqual(point2str(obj.location), point2str(location))

        # Client handshake
        test_client.publish(
            'user/{}/client/{}/status'.format(username, client_id),
            ClientStatus.F.name)

        # process messages
        self.loop(test_client, subscribe_client)

        # check record
        clnt = Client.objects.get(client_id=client_id)
        self.assertEqual(clnt.status, ClientStatus.F.name)
        self.assertEqual(clnt.ambulance, None)
        self.assertEqual(clnt.hospital, None)

        # wait for disconnect
        test_client.wait()
        subscribe_client.wait()
        django_client.logout()
Ejemplo n.º 27
0
 def test_dataset_post_no_dataset(self):
     client = DjangoClient()
     response = client.post(reverse('datasets'), data={})
     self.assertEqual(400, response.status_code)
     self.assertEqual(b'"Missing dataset name"', response.content)
Ejemplo n.º 28
0
    def test_create_authorised_representative(self):
        # We need an accepted invitation to be able to create a client
        usr = UserFactory.create()
        FirmEmailInviteFactory.create(user=usr,
                                      status=FirmEmailInvite.STATUS_ACCEPTED)
        url = reverse('api:v1:firm:authorisedrepresentative-list')
        address = {
            "address": "123 My Street\nSome City",
            "post_code": "112233",
            "region": {
                "name": "California",
                "country": "US",
                "code": "CA",
            }
        }
        regional_data = {
            'ssn': '555-55-5555',
        }
        firm_details = {
            'office_address': address,
            'postal_address': address,
            'daytime_phone_num': '+1-234-234-2342',
            'alternate_email_address': usr.email,
            'site_url': 'http://firm.example.com',
        }
        advisors = [{
            'first_name': 'Advisor',
            'last_name': 'Example1',
            'email': '*****@*****.**',
            'work_phone_num': '+1-234-234-2342',
        }, {
            'first_name': 'Advisor',
            'last_name': 'Example2',
            'email': '*****@*****.**',
            'work_phone_num': '+1-234-234-2342',
        }]
        firm = {
            'name': 'Test Firm',
            'slug': 'test-firm',
            'token': generate_token(),
            'details': firm_details,
        }
        data = {
            "betasmartz_agreement": True,
            "date_of_birth": date(2016, 9, 21),
            "gender": GENDER_MALE,
            "phone_num": "+1-234-234-2342",
            "residential_address": address,
            "regional_data": json.dumps(regional_data),
            "firm": firm,
            'advisors': advisors
        }
        self.client.force_authenticate(usr)
        response = self.client.post(url, data)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        # Ensure it is created approved and accepted.
        self.assertEqual(response.data['is_confirmed'], True)
        self.assertEqual(response.data['is_accepted'], True)
        self.assertEqual(response.data['phone_num'], "+12342342342")
        self.assertEqual(response.data['residential_address']['address'],
                         address['address'])
        self.assertEqual(response.data['residential_address']['post_code'],
                         address['post_code'])
        self.assertEqual(response.data['betasmartz_agreement'], True)
        regional_data_load = json.loads(response.data['regional_data'])
        self.assertEqual(regional_data_load['ssn'], regional_data['ssn'])

        # check onboarding status is complete
        lookup_invite = FirmEmailInvite.objects.get(user=usr)
        self.assertEqual(lookup_invite.status, FirmEmailInvite.STATUS_COMPLETE)

        lookup_firm = Firm.objects.get(pk=response.data['firm']['id'])
        self.assertEqual(lookup_firm.name, 'Test Firm')
        self.assertEqual(lookup_firm.details.daytime_phone_num, '+12342342342')

        # can login with new firm
        self.client = DjangoClient()  # django
        url = reverse('login')
        data = {
            'username': usr.email,
            'password': '******',
        }
        response = self.client.post(url, data)
        # redirect to firm application
        self.assertRedirects(response, reverse('firm:overview'))

        # can retrieve profile info ok
        url = reverse('api:v1:user-me')
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        # advisors created
        advisors = Advisor.objects.filter(firm=lookup_firm, )
        self.assertEqual(len(advisors), 2, msg='Created all advisors')
        user1 = User.objects.filter(email='*****@*****.**')
        self.assertEqual(len(user1), 1, msg='Advisor User 1 exists')
        user2 = User.objects.filter(email='*****@*****.**')
        self.assertEqual(len(user2), 1, msg='Advisor User 2 exists')
Ejemplo n.º 29
0
 def test_list_estimators(self):
     client = DjangoClient()
     response = client.get(reverse('estimators_list'))
     self.assertEqual(200, response.status_code)
     self.assertEqual(len(response.data), len(all_estimators()))
Ejemplo n.º 30
0
 def test_estimator_detail_bad(self):
     client = DjangoClient()
     response = client.get(
         reverse('estimator_detail', kwargs={'clf': 'alice and bob'}))
     self.assertEqual(200, response.status_code)
     self.assertEqual(len(response.data), 3)