def test_user_can_access_their_own_resource(self): test_objects = TestObjects() test_user = test_objects.get_test_user() timer = Timer(notes="More testing, boss", user=test_user) timer.save() assert(can_access_user_owned_resource(test_user, timer)) timer.delete()
def test_account_admin_cannot_access_resource_if_account_different(self): test_objects = TestObjects() test_user = test_objects.get_test_user() timer = Timer(notes="More testing, boss", user=test_user) timer.save() assert(can_access_user_owned_resource(test_user, timer)) timer.delete()
def test_eval_ok(self): user = TestObjects().get_test_user() t1 = Timer(id=ObjectId(b"Timer1Timer2"), notes="I want a shrubbery", user=user) # print(t1.__repr__()) t2 = eval(t1.__repr__()) # Note this part works partly because compare is brain-dead, compares id only and only works for non-null id # But that may be what we need for MongoEngine purposes, so don't override assert(t1 == t2) # A better check assert(t1.__repr__() == t2.__repr__())
def test_can_save_and_load_timer(self): user = TestObjects().get_test_user() t = Timer(id=TestObjects().get_any_id(), notes="Saved from unit test", user=user) t.save() t2 = Timer.objects(id = t.id).first() assert(t.__repr__() == t2.__repr__()) t.delete()
def test_user_not_updated_on_save(self): user = TestObjects().get_test_user() t1 = Timer(id=ObjectId(b"Timer1Timer3"), notes="I want a shrubbery", user=user) t1.save() t1.user.password = "******" t1.save() # TODO ETC... t1.delete()
def test_timer_resource_all(self): token = test_credentials.get_auth_token() user = TestObjects().get_test_user() timer = Timer(notes="Just a test timer", user=user, tags=["Unit Tests"], seconds = 22, running = True) timer_dict = TimerFormatter().model_to_dict(timer) # Not authorized w/o token response = requests.post(v1_api + "/timer", headers={'content-type' : 'application/json'}, data=timer.to_json()) assert(response.status_code == http.client.UNAUTHORIZED) # With token, should get "CREATED" response = requests.post(v1_api + "/timer", headers={'content-type' : 'application/json'}, auth=token, data=timer.to_json()) assert(response.status_code == http.client.CREATED) # Object location url = response.headers["Location"] # Not authorized without the token (401) response = requests.get(url) assert(response.status_code == http.client.UNAUTHORIZED) # Re-send the request with the token, this time get OK (200) response = requests.get(url, auth=token) assert(response.status_code == http.client.OK) timer_dict2 = loads(response.text) assert(timer_dict2["seconds"] == 22) # Update the seconds and PUT the request timer_dict2["seconds"] = 99 response = requests.put(url, headers={'content-type' : 'application/json'}, auth=token, data=dumps(timer_dict2)) assert(response.status_code == http.client.OK) response = requests.get(url, auth=token) assert(response.status_code == http.client.OK) timer_dict3 = loads(response.text) assert(timer_dict3["seconds"] == 99) # Currently our GET all means "Everything for the current user. This will change, and we'll need a new test get_many_url = v1_api + "/timer" # Not authorized without the token (401) response = requests.get(get_many_url) assert(response.status_code == http.client.UNAUTHORIZED) # Re-send the request with the token, this time get OK (200) response = requests.get(get_many_url, auth=token) assert(response.status_code == http.client.OK) print(response.text) # Delete the resource response = requests.delete(url, auth=token) assert(response.status_code == http.client.NO_CONTENT)
def test_can_dump_and_load_timer(self): user = TestObjects().get_test_user() timer = Timer(notes="Just a test timer", user=user, tags=["Unit Tests"], seconds = 22, running = True) timer.save() tf = TimerFormatter() timer_entity_as_dict = tf.model_to_dict(timer) timer.delete() timer2 = tf.dict_to_model(Timer, timer_entity_as_dict) # This won't pass, there are subtle, trivial differences in datetimes once dates have been serialized #assert(timer.lastRestart == timer2.lastRestart) #assert(timer.dateEntered == timer2.dateEntered) assert(timer.tags == timer2.tags) assert(timer.running == timer2.running) assert(timer.seconds == timer2.seconds) assert(timer.notes == timer2.notes) assert(timer.user == timer2.user) assert(timer.to_json() == timer2.to_json())