Esempio n. 1
0
 def setUp(self):
     ''' Test setup '''
     
     self.browser = Browser('phantomjs')
     
     # if we don't set the window size, we won't be able to interact with UI that might 
     # be hidden because of our media queries with a smaller emulated window
     self.browser.driver.set_window_size(1120, 550)
     
     # set up the tables in the database
     Base.metadata.create_all(engine)
     
     # create an example user
     self.user = User(name = 'capnmorgan', email = '*****@*****.**',
         password = generate_password_hash('test'))
     session.add(self.user)
     session.commit()
     
     # create a second example user
     self.user = User(name = 'Bob', email = '*****@*****.**',
         password = generate_password_hash('test'))
     session.add(self.user)
     session.commit()
     
     self.process = multiprocessing.Process(target = app.run, kwargs = { 'port': 7000 })
     self.process.start()
     time.sleep(1)
Esempio n. 2
0
def importdata():
    ''' import data from data.json '''
    with open('data.json') as data_file:
        data = json.load(data_file)
    
    for collection in data:
        model_name = collection['model_name']
        data_records = collection['data']
        count = 0
        for data_record in data_records:
            try:
                model = getattr(database, model_name)
                item = model(**data_record)
                session.add(item)
                session.commit()
            except Exception as e:
                print(type(e), 'for {}'.format(model_name))
            else:
                count += 1
                pass
                
        if count == len(data_records):
            print('Successfully imported all {} records of type {}'.format(count, model_name))
        else:
            print('{} records of {} were imported of type {}'.format(count, len(data_records), model_name))
 def test_get_new_game(self):
     ''' get a new game for a user '''
     pw = 'notsecret'
     bob = database.User(name = 'ferrycapn', email = '*****@*****.**', password = generate_password_hash(pw))
     
     session.add(bob)
     session.commit()
     
     token = self.get_jwt(bob.name, pw)
     
     response = self.client.post('/api/games',
          headers = [
             ('Accept', 'application/json'),
             ('Authorization', 'JWT ' + token)
         ]
     )
     
     self.assertEqual(response.status_code, 201)
     self.assertEqual(response.mimetype, 'application/json')
     
     data = json.loads(response.data.decode('ascii'))
     self.assertEqual(len(data), 8)
     
     self.assertEqual(data['player']['id'], bob.id)
     self.assertLessEqual(data['created_date'], unix_timestamp(datetime.now()))
     self.assertEqual(data['cash_available'], 1000000)
     self.assertEqual(data['current_week'], 1)
     self.assertEqual(data['current_year'], 2000)
 def test_register_already_exists_name(self):
     ''' test the user registration endpoint of the API where the user
         already exists '''
     user = database.User(
         email = '*****@*****.**', 
         password = '******',
         name = 'BestBureaucrat'
     )
     session.add(user)
     session.commit()
     
     data = {
         "email": "*****@*****.**",
         "password": "******",
         "name": "BestBureaucrat"
     }
     
     response = self.client.post('/api/register',
         data = json.dumps(data),
         content_type = 'application/json',
         headers = [('Accept', 'application/json')]
     )
     
     self.assertEqual(response.status_code, 409)
     self.assertEqual(response.mimetype, 'application/json')
     
     data = json.loads(response.data.decode('ascii'))
     self.assertEqual(data['status'], 'Error')
     self.assertEqual(data['data'], None)
     self.assertEqual(data['message'], 'This username is already being used.')
Esempio n. 5
0
    def setUp(self):
        """ test setup """
        self.client = app.test_client()

        # set up the tables in the database
        Base.metadata.create_all(engine)
        
        # create an example user
        self.user = database.User(name = 'capnmorgan', email = '*****@*****.**', 
            password = generate_password_hash('test'))
        session.add(self.user)
        session.commit()
Esempio n. 6
0
 def setUp(self):
     ''' test setup '''
     self.client = app.test_client()
     
     # set up the tables in the database
     Base.metadata.create_all(engine)
     
     # create an example user
     self.user = User(name = 'Alice', email = '*****@*****.**', 
         password = generate_password_hash('test'))
     session.add(self.user)
     session.commit()
Esempio n. 7
0
def adduser():
    name = input('Name: ')
    email = input('Email: ')
    if session.query(User).filter_by(email = email).first():
        print('User with that email address already exists.')
        return
    
    password = ''
    password_2 = ''
    while len(password) < 8 or password != password_2:
        password = getpass('Password: '******'Re-enter password: ')
    user = User(name = name, email = email, password = generate_password_hash(password))
    session.add(user)
    session.commit()
 def test_login(self):
     ''' test a successful login '''
     user = database.User(
         email = '*****@*****.**', 
         password = generate_password_hash('notsecret'),
         name = 'BestBureaucrat'
     )
     session.add(user)
     session.commit()
     
     data = {
         "name": "BestBureaucrat",
         "password": "******"
     }
     
     response = self.client.post(app.config['JWT_AUTH_URL_RULE'],
         data = json.dumps(data),
         content_type = 'application/json',
         headers = [('Accept', 'application/json')]
     )
     
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.mimetype, 'application/json')
     
     data = json.loads(response.data.decode('ascii'))
     token = data['access_token']
     
     response = self.client.get('/api/user',
         data = json.dumps(data),
         content_type = 'application/json',
         headers = [
             ('Accept', 'application/json'),
             ('Authorization', 'JWT ' + token)
         ]
     )
     
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.mimetype, 'application/json')
     
     data = json.loads(response.data.decode('ascii'))
     self.assertEqual(data['id'], 2)
     self.assertEqual(data['name'], 'BestBureaucrat')