Beispiel #1
0
class CommandTests(TestCase):
    def setUp(self):
        self.client = MyTestClient()
        self.client.post(newt_base_url + "/auth", data=login)

    def test_root(self):
        r = self.client.get(newt_base_url + '/command')
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['status'], "OK")
        self.assertIn(machine, json_response['output'])

    def test_command(self):
        r = self.client.post(newt_base_url + '/command/' + machine,
                             {'command': '/bin/hostname'})
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['status'], "OK")
        self.assertIsNotNone(json_response['output']['output'])
        self.assertEquals(json_response['output']['retcode'], 0)

    @skipIf(machine != "localhost", "Can't run ls on remote machine")
    def test_command_with_args(self):
        # Run ls in /
        r = self.client.post(newt_base_url + '/command/' + machine,
                             {'command': '/bin/ls -a /'})
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['status'], "OK")
        # os.listdir() leaves off . and .. so add them in
        files = ['.', '..'] + os.listdir('/')
        files.sort()
        newtfiles = json_response['output']['output'].strip().split('\n')
        newtfiles.sort()
        self.assertEquals(files, newtfiles)
Beispiel #2
0
class CommandTests(TestCase):
    def setUp(self):
        self.client = MyTestClient()
        self.client.post(newt_base_url + "/auth", data=login)

    def test_root(self):
        r = self.client.get(newt_base_url+'/command')
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['status'], "OK")
        self.assertIn(machine, json_response['output'])

    def test_command(self):
        r = self.client.post(newt_base_url+'/command/'+machine, {'command': '/bin/hostname'})
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['status'], "OK")
        self.assertIsNotNone(json_response['output']['output'])
        self.assertEquals(json_response['output']['retcode'], 0)

    @skipIf(machine != "localhost", "Can't run ls on remote machine")
    def test_command_with_args(self):
        # Run ls in / 
        r = self.client.post(newt_base_url+'/command/'+machine, {'command': '/bin/ls -a /'})
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['status'], "OK")
        # os.listdir() leaves off . and .. so add them in
        files = ['.', '..'] + os.listdir('/')
        files.sort()
        newtfiles = json_response['output']['output'].strip().split('\n')
        newtfiles.sort()
        self.assertEquals(files, newtfiles)
Beispiel #3
0
class AcctTests(TestCase):
    fixtures = ["test_fixture.json"]

    def setUp(self):
        self.client = MyTestClient()

    def test_info_ret(self):
        self.client.post(newt_base_url + "/auth", data=login)

        r = self.client.get(newt_base_url +
                            "/account/user/%s/" % login['username'])
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['username'],
                          login['username'])

        r = self.client.get(newt_base_url + "/account/user/id/2/")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['username'],
                          login['username'])

        r = self.client.get(newt_base_url + "/account/group/id/1/")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['name'], "Test Group")
Beispiel #4
0
class StatusTests(TestCase):
    def setUp(self):
        self.client = MyTestClient()

    def test_all(self):
        r = self.client.get(newt_base_url+'/status')
        self.assertEquals(r.status_code, 200)

        json_response = r.json()
        self.assertEquals(json_response['status'], "OK")

        for x in json_response['output']:
            if x['system'] == "localhost":
                self.assertEquals(x['status'], 'up')
            else:
                self.assertIn(x['status'], ['up', 'down'])

    def test_one(self):
        system = settings.NEWT_CONFIG['SYSTEMS'][0]['NAME']
        r = self.client.get('%s/status/%s' % (newt_base_url, system))
        self.assertEquals(r.status_code, 200)

        json_response = r.json()
        self.assertEquals(json_response['status'], "OK")

        self.assertEquals(json_response['output']['status'], 'up')
Beispiel #5
0
class StatusTests(TestCase):
    def setUp(self):
        self.client = MyTestClient()

    def test_all(self):
        r = self.client.get(newt_base_url + '/status')
        self.assertEquals(r.status_code, 200)

        json_response = r.json()
        self.assertEquals(json_response['status'], "OK")

        for x in json_response['output']:
            if x['system'] == "localhost":
                self.assertEquals(x['status'], 'up')
            else:
                self.assertIn(x['status'], ['up', 'down'])

    def test_one(self):
        system = settings.NEWT_CONFIG['SYSTEMS'][0]['NAME']
        r = self.client.get('%s/status/%s' % (newt_base_url, system))
        self.assertEquals(r.status_code, 200)

        json_response = r.json()
        self.assertEquals(json_response['status'], "OK")

        self.assertEquals(json_response['output']['status'], 'up')
Beispiel #6
0
 def setUp(self):
     self.client = MyTestClient()
     # Hacky: Need to figure out a way around this...
     try:
         from pymongo import MongoClient
         db = MongoClient()['store']
         db.test_store_1.drop()
         db.permissions.remove({"name": "test_store_1"})
     except Exception:
         pass
     try:
         import redis
         storedb = redis.Redis(host="localhost", db=0)
         storedb.flushdb()
     except Exception:
         pass
Beispiel #7
0
class AcctTests(TestCase):
    fixtures = ["test_fixture.json"]

    def setUp(self):
        self.client = MyTestClient()

    def test_info_ret(self):
        r = self.client.get(newt_base_url + "/account/user/%s/" % login['username'])
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['username'], login['username'])

        r = self.client.get(newt_base_url + "/account/user/id/2/")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['username'], login['username'])

        r = self.client.get(newt_base_url + "/account/group/id/1/")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['name'], "Test Group")
Beispiel #8
0
 def setUp(self):
     self.client = MyTestClient()
     # Hacky: Need to figure out a way around this...
     try:
         from pymongo import MongoClient
         db = MongoClient()['stores']
         db.test_store_1.drop()
         db.permissions.remove({"name":"test_store_1"})
     except Exception:
         pass
     try:
         import redis
         storedb = redis.Redis(host="localhost", db=0)
         storedb.flushdb()
     except Exception:
         pass
Beispiel #9
0
class AuthTests(TestCase):
    fixtures = ["test_fixture.json"]

    def setUp(self):
        self.client = MyTestClient()

    def test_login(self):
        # Should not be logged in
        #print ( ' test not log in ? ')
        r = self.client.get(newt_base_url + "/auth")
        #print ('get ok ?' ,r.status_code )
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        #print ( 'json' ,  json_response  )
        self.assertEquals(json_response['output']['auth'], False)

        # Should be logged in
        r = self.client.post(newt_base_url + "/auth", data=login)
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        #print('cookies 1 : ' ,  self.client.cookies )
        self.assertEquals(json_response['output']['auth'], True)
        self.assertEquals(json_response['output']['username'],
                          login['username'])
        cookies = self.client.cookies
        # Loggen in self.client should return user info
        #print( 'session id :' , json_response["output"]["newt_sessionid"])
        #self.client.session['newt_sessionid'] =  json_response["output"]["newt_sessionid"]
        r = self.client.get(newt_base_url + "/auth")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        #print( 'cookies 2 : ' , self.client.cookies )
        self.assertEquals(json_response['output']['auth'], True)
        self.assertEquals(json_response['output']['username'],
                          login['username'])

    def test_logout(self):
        # Should be logged in
        r = self.client.post(newt_base_url + "/auth", data=login)
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['auth'], True)
        self.assertEquals(json_response['output']['username'],
                          login['username'])

        r = self.client.delete(newt_base_url + "/auth")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['auth'], False)

        r = self.client.get(newt_base_url + "/auth")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['auth'], False)
Beispiel #10
0
class AuthTests(TestCase):
    fixtures = ["test_fixture.json"]

    def setUp(self):
        self.client = MyTestClient()

    def test_login(self):
        # Should not be logged in
        r = self.client.get(newt_base_url + "/auth")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['auth'], False)

        # Should be logged in
        r = self.client.post(newt_base_url + "/auth", data=login)
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['auth'], True)
        self.assertEquals(json_response['output']['username'],
                          login['username'])

        # Loggen in self.client should return user info
        r = self.client.get(newt_base_url + "/auth")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['auth'], True)
        self.assertEquals(json_response['output']['username'],
                          login['username'])

    def test_logout(self):
        # Should be logged in
        r = self.client.post(newt_base_url + "/auth", data=login)
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['auth'], True)
        self.assertEquals(json_response['output']['username'],
                          login['username'])

        r = self.client.delete(newt_base_url + "/auth")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['auth'], False)

        r = self.client.get(newt_base_url + "/auth")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['auth'], False)
Beispiel #11
0
class AuthTests(TestCase):
    fixtures = ["test_fixture.json"]

    def setUp(self):
        self.client = MyTestClient()

    def test_login(self):
        # Should not be logged in
        r = self.client.get(newt_base_url + "/auth")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['auth'], False)
        
        # Should be logged in
        r = self.client.post(newt_base_url + "/auth", data=login)
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['auth'], True)
        self.assertEquals(json_response['output']['username'], login['username'])

        # Loggen in self.client should return user info
        r = self.client.get(newt_base_url + "/auth")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['auth'], True)
        self.assertEquals(json_response['output']['username'], login['username'])


    def test_logout(self):
        # Should be logged in
        r = self.client.post(newt_base_url + "/auth", data=login)
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['auth'], True)
        self.assertEquals(json_response['output']['username'], login['username'])

        r = self.client.delete(newt_base_url + "/auth")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['auth'], False)

        r = self.client.get(newt_base_url + "/auth")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['auth'], False)
Beispiel #12
0
class JobTests(TestCase):
    fixtures = ["test_fixture.json"]

    def setUp(self):
        self.client = MyTestClient()
        self.client.post(newt_base_url + "/auth", data=login)

    def test_get_queues(self):
        # Tests getting queues
        r = self.client.get(newt_base_url + "/job/")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertTrue(len(json_response['output']) > 0)
        self.assertIn(machine, json_response['output'].keys())

    def test_running_cmds(self):

        # Tests submitting a job
        payload = {"jobscript": "/bin/hostname\nsleep 10"}
        r = self.client.post(newt_base_url + "/job/" + machine + "/",
                             data=payload)
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertIsNot(json_response['output']['jobid'], None)

        # Get job id from submitting the job
        job_id = json_response['output']['jobid']

        # Give the process time to register
        time.sleep(1)

        # Tests getting job info
        r = self.client.get(newt_base_url + "/job/" + machine +
                            "/%s/" % job_id)
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['jobid'], job_id)
        self.assertEquals(json_response['output']['user'], login['username'])

        # Delete job from queue
        r = self.client.delete(newt_base_url + "/job/" + machine +
                               "/%s/" % job_id)
        self.assertEquals(r.status_code, 200)
Beispiel #13
0
class JobTests(TestCase):
    fixtures = ["test_fixture.json"]
    

    def setUp(self):
        self.client = MyTestClient()
        self.client.post(newt_base_url + "/auth", data=login)

    def test_get_queues(self):
        # Tests getting queues
        r = self.client.get(newt_base_url + "/job/")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertTrue(len(json_response['output']) > 0)
        self.assertIn(machine, json_response['output'].keys())

    def test_running_cmds(self):

        # Tests submitting a job
        payload = {
            "jobscript": "/bin/hostname\nsleep 10"
        }
        r = self.client.post(newt_base_url + "/job/"+machine+"/", data=payload)
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertIsNot(json_response['output']['jobid'], None)

        # Get job id from submitting the job
        job_id = json_response['output']['jobid']

        # Give the process time to register
        time.sleep(1)  

        # Tests getting job info
        r = self.client.get(newt_base_url + "/job/"+machine+"/%s/" % job_id)
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['jobid'], job_id)
        self.assertEquals(json_response['output']['user'], login['username'])
        
        # Delete job from queue
        r = self.client.delete(newt_base_url + "/job/"+machine+"/%s/" % job_id)
        self.assertEquals(r.status_code, 200)
Beispiel #14
0
class FileTests(TestCase):
    def setUp(self):
        self.client = MyTestClient()
        self.client.post(newt_base_url + "/auth", data=login)

    def test_root(self):
        r = self.client.get(newt_base_url + '/file')
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['status'], "OK")
        self.assertIn(machine, json_response['output'])

    def test_getdir(self):
        r = self.client.get(newt_base_url + '/file/' + machine + "/")
        self.assertEquals(r.status_code, 200)

        json_response = r.json()
        self.assertEquals(json_response['status'], "OK")
        print(json_response['output'])
        self.assertTrue(len(json_response['output']) >= 2)
        self.assertEquals(json_response['output'][0]['name'], ".")
        self.assertEquals(json_response['output'][1]['name'], "..")

    def test_uploadfile(self):
        rand_string = '%010x' % random.randrange(16**10)
        r = self.client.put(newt_base_url + "/file/" + machine +
                            "/tmp/tmp_newt_2.txt",
                            data=rand_string)
        self.assertEqual(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['location'],
                          "/tmp/tmp_newt_2.txt")
        r = self.client.get(newt_base_url + '/file/' + machine +
                            '/tmp/tmp_newt_2.txt?download=true')
        self.assertEquals(r.status_code, 200)
        self.assertEquals(
            list(r.streaming_content)[0].decode('utf-8'), rand_string)
        #self.assertEquals(r.streaming_content.read(8192), rand_string)
        try:
            os.remove("/tmp/tmp_newt_2.txt")
        except Exception:
            pass
Beispiel #15
0
class FileTests(TestCase):
    def setUp(self):
        self.client = MyTestClient()
        self.client.post(newt_base_url + "/auth", data=login)

    def test_root(self):
        r = self.client.get(newt_base_url+'/file')
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['status'], "OK")
        self.assertIn(machine, json_response['output'])
        
    def test_getdir(self):        
        r = self.client.get(newt_base_url+'/file/'+machine+"/")
        self.assertEquals(r.status_code, 200)

        json_response = r.json()
        self.assertEquals(json_response['status'], "OK")
        
        self.assertTrue(len(json_response['output']) >= 2)
        self.assertEquals(json_response['output'][0]['name'], ".")
        self.assertEquals(json_response['output'][1]['name'], "..")
        
    def test_uploadfile(self):
        rand_string = '%010x' % random.randrange(16**10)
        r = self.client.put(newt_base_url + "/file/"+machine+"/tmp/tmp_newt_2.txt", data=rand_string)
        self.assertEqual(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['location'], "/tmp/tmp_newt_2.txt")
        r = self.client.get(newt_base_url+'/file/'+machine+'/tmp/tmp_newt_2.txt?download=true')
        self.assertEquals(r.status_code, 200)
        self.assertEquals(r.streaming_content.next(), rand_string)
        try:
            os.remove("/tmp/tmp_newt_2.txt")
        except Exception:
            pass
Beispiel #16
0
class StoresTests(TestCase):
    fixtures = ["test_fixture.json"]

    def setUp(self):
        self.client = MyTestClient()
        # Hacky: Need to figure out a way around this...
        try:
            from pymongo import MongoClient
            db = MongoClient()['stores']
            db.test_store_1.drop()
            db.permissions.remove({"name":"test_store_1"})
        except Exception:
            pass
        try:
            import redis
            storedb = redis.Redis(host="localhost", db=0)
            storedb.flushdb()
        except Exception:
            pass

    def test_stores_basic(self):
        r = self.client.get(newt_base_url + "/stores")
        self.assertEquals(r.status_code, 200)

    def test_stores_manipulation(self):
        # Creates a new store (create_store)
        r = self.client.post(newt_base_url + "/stores")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()

        # Ensures that no data was added to the store
        self.assertEquals(json_response['output']['oid'], [])
        store_id = json_response['output']['id']
        
        # Ensures that new store is empty (get_store_contents)
        r = self.client.get(newt_base_url + "/stores/" + store_id + "/")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output'], [])
        
        # Tests insertion (store_insert)
        payload = {"data": json.dumps({"foo":"bar"})}
        r = self.client.post(newt_base_url + "/stores/" + store_id + "/",
                          data=payload)
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        obj_id = json_response['output']

        # Checks insertion by checking all of the store's objects (get_store_contents)
        r = self.client.get(newt_base_url + "/stores/" + store_id + "/")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output'][0]['data'], payload['data'])
        self.assertEquals(json_response['output'][0]['oid'], obj_id)

        # Checks insertion by checking the individual object (store_get_obj)
        r = self.client.get(newt_base_url + "/stores/" + store_id + "/" + obj_id + "/")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output'], payload['data'])
        
        # Tests update (store_update)
        updated_payload = {"data": json.dumps({"foo": "baz"})}
        r = self.client.put(newt_base_url + "/stores/" + store_id + "/" + obj_id + "/",
                         data=json.dumps(updated_payload), content_type="application/json")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output'], obj_id)

        # Checks updated data
        r = self.client.get(newt_base_url + "/stores/" + store_id + "/" + obj_id + "/")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output'], updated_payload['data'])

        # Tests delete
        r = self.client.delete(newt_base_url + "/stores/" + store_id + "/")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEqual(json_response['output'], store_id)

        # Ensures that getting the deleted store will error
        r = self.client.get(newt_base_url + "/stores/" + store_id + "/")
        self.assertEquals(r.status_code, 404)        

    def test_stores_creation_with_initial(self):
        payload = {"data": json.dumps({"x":5})}

        # Without an initial name
        r = self.client.post(newt_base_url + "/stores/", data=payload)
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        store_id = json_response['output']['id']
        self.assertEquals(len(json_response['output']['oid']), 1)
        obj_id = json_response['output']['oid'][0]

        # Checks the data
        r = self.client.get(newt_base_url + "/stores/" + store_id + "/" + obj_id + "/")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output'], payload['data'])


        # With an initial name
        r = self.client.post(newt_base_url + "/stores/teststore1/", data=payload)
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['id'], "teststore1")
        self.assertEquals(len(json_response['output']['oid']), 1)
        obj_id = json_response['output']['oid'][0]

        # Checks the data
        r = self.client.get(newt_base_url + "/stores/teststore1/" + obj_id + "/")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output'], payload['data'])

        # Deletes the stores
        r = self.client.delete(newt_base_url + "/stores/teststore1/")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEqual(json_response['output'], "teststore1")
        r = self.client.delete(newt_base_url + "/stores/" + store_id + "/")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEqual(json_response['output'], store_id)

    def test_store_perms(self):
        self.client.post(newt_base_url + "/auth", data=login)

        # Create a store
        r = self.client.post(newt_base_url + "/stores/test_store_1/")
        self.assertEquals(r.status_code, 200)
        r = self.client.get(newt_base_url + "/stores/test_store_1/perms/")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        # Checks that the creator has appropriate permissions
        self.assertEquals(json_response['output']['name'], "test_store_1")
        self.assertEquals(json_response['output']['perms'][0]['name'], login['username'])
        self.assertIn("r", json_response['output']['perms'][0]['perms'])
        self.assertIn("w", json_response['output']['perms'][0]['perms'])

        payload = {"data": json.dumps([{"name": login['username'], "perms": ["r", "w", "x"]}])}
        r = self.client.post(newt_base_url + "/stores/test_store_1/perms/", data=payload)
        self.assertEqual(r.status_code, 200)

        r = self.client.get(newt_base_url + "/stores/test_store_1/perms/")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['name'], "test_store_1")
        self.assertEquals(json_response['output']['perms'][0]['name'], login['username'])
        self.assertIn("r", json_response['output']['perms'][0]['perms'])
        self.assertIn("w", json_response['output']['perms'][0]['perms'])
        self.assertIn("x", json_response['output']['perms'][0]['perms'])

        # Deletes the stores
        r = self.client.delete(newt_base_url + "/stores/test_store_1/")
        self.assertEquals(r.status_code, 200)
        r = self.client.get(newt_base_url + "/stores/test_store_1/perms/")
        self.assertEquals(r.status_code, 404)
Beispiel #17
0
class StoreTests(TestCase):
    fixtures = ["test_fixture.json"]

    def setUp(self):
        self.client = MyTestClient()
        # Hacky: Need to figure out a way around this...
        try:
            from pymongo import MongoClient
            db = MongoClient()['store']
            db.test_store_1.drop()
            db.permissions.remove({"name": "test_store_1"})
        except Exception:
            pass
        try:
            import redis
            storedb = redis.Redis(host="localhost", db=0)
            storedb.flushdb()
        except Exception:
            pass

    def test_store_basic(self):
        self.client.post(newt_base_url + "/auth", data=login)

        r = self.client.get(newt_base_url + "/store")
        self.assertEquals(r.status_code, 200)

    def test_store_manipulation(self):
        self.client.post(newt_base_url + "/auth", data=login)

        # Creates a new store (create_store)
        r = self.client.post(newt_base_url + "/store")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()

        # Ensures that no data was added to the store
        self.assertEquals(json_response['output']['oid'], [])
        store_id = json_response['output']['id']

        # Ensures that new store is empty (get_store_contents)
        r = self.client.get(newt_base_url + "/store/" + store_id + "/")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output'], [])

        # Tests insertion (store_insert)
        payload = {"data": json.dumps({"foo": "bar"})}
        r = self.client.post(newt_base_url + "/store/" + store_id + "/",
                             data=payload)
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        obj_id = json_response['output']

        # Checks insertion by checking all of the store's objects (get_store_contents)
        r = self.client.get(newt_base_url + "/store/" + store_id + "/")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output'][0]['data'], payload['data'])
        self.assertEquals(json_response['output'][0]['oid'], obj_id)

        # Checks insertion by checking the individual object (store_get_obj)
        r = self.client.get(newt_base_url + "/store/" + store_id + "/" +
                            obj_id + "/")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output'], payload['data'])

        # Tests update (store_update)
        updated_payload = {"data": json.dumps({"foo": "baz"})}
        r = self.client.put(newt_base_url + "/store/" + store_id + "/" +
                            obj_id + "/",
                            data=json.dumps(updated_payload),
                            content_type="application/json")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output'], obj_id)

        # Checks updated data
        r = self.client.get(newt_base_url + "/store/" + store_id + "/" +
                            obj_id + "/")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output'], updated_payload['data'])

        # Tests delete
        r = self.client.delete(newt_base_url + "/store/" + store_id + "/")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEqual(json_response['output'], store_id)

        # Ensures that getting the deleted store will error
        r = self.client.get(newt_base_url + "/store/" + store_id + "/")
        self.assertEquals(r.status_code, 404)

    def test_store_creation_with_initial(self):
        self.client.post(newt_base_url + "/auth", data=login)

        payload = {"data": json.dumps({"x": 5})}

        # Without an initial name
        r = self.client.post(newt_base_url + "/store/", data=payload)
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        store_id = json_response['output']['id']
        self.assertEquals(len(json_response['output']['oid']), 1)
        obj_id = json_response['output']['oid'][0]

        # Checks the data
        r = self.client.get(newt_base_url + "/store/" + store_id + "/" +
                            obj_id + "/")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output'], payload['data'])

        # With an initial name
        r = self.client.post(newt_base_url + "/store/teststore1/",
                             data=payload)
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['id'], "teststore1")
        self.assertEquals(len(json_response['output']['oid']), 1)
        obj_id = json_response['output']['oid'][0]

        # Checks the data
        r = self.client.get(newt_base_url + "/store/teststore1/" + obj_id +
                            "/")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output'], payload['data'])

        # Deletes the store
        r = self.client.delete(newt_base_url + "/store/teststore1/")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEqual(json_response['output'], "teststore1")
        r = self.client.delete(newt_base_url + "/store/" + store_id + "/")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEqual(json_response['output'], store_id)

    def test_store_perms(self):
        self.client.post(newt_base_url + "/auth", data=login)

        # Create a store
        r = self.client.post(newt_base_url + "/store/test_store_1/")
        self.assertEquals(r.status_code, 200)
        r = self.client.get(newt_base_url + "/store/test_store_1/perms/")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        # Checks that the creator has appropriate permissions
        self.assertEquals(json_response['output']['name'], "test_store_1")
        self.assertEquals(json_response['output']['perms'][0]['name'],
                          login['username'])
        self.assertIn("r", json_response['output']['perms'][0]['perms'])
        self.assertIn("w", json_response['output']['perms'][0]['perms'])

        payload = {
            "data":
            json.dumps([{
                "name": login['username'],
                "perms": ["r", "w", "x"]
            }])
        }
        r = self.client.post(newt_base_url + "/store/test_store_1/perms/",
                             data=payload)
        self.assertEqual(r.status_code, 200)

        r = self.client.get(newt_base_url + "/store/test_store_1/perms/")
        self.assertEquals(r.status_code, 200)
        json_response = r.json()
        self.assertEquals(json_response['output']['name'], "test_store_1")
        self.assertEquals(json_response['output']['perms'][0]['name'],
                          login['username'])
        self.assertIn("r", json_response['output']['perms'][0]['perms'])
        self.assertIn("w", json_response['output']['perms'][0]['perms'])
        self.assertIn("x", json_response['output']['perms'][0]['perms'])

        # Deletes the store
        r = self.client.delete(newt_base_url + "/store/test_store_1/")
        self.assertEquals(r.status_code, 200)
        r = self.client.get(newt_base_url + "/store/test_store_1/perms/")
        self.assertEquals(r.status_code, 404)
Beispiel #18
0
 def setUp(self):
     self.client = MyTestClient()
     self.client.post(newt_base_url + "/auth", data=login)
Beispiel #19
0
 def setUp(self):
     self.client = MyTestClient()
Beispiel #20
0
 def setUp(self):
     self.client = MyTestClient()
     self.client.post(newt_base_url + "/auth", data=login)
Beispiel #21
0
 def setUp(self):
     self.client = MyTestClient()