def test_userCanAffordValue_returns_true(self): user = User() user.resources.science._value = 100 bal = Balance(science=Cost(-10,0)) val = user.resources.science._value print val self.assertTrue(user.purchase(balance=bal))
def createUser(name, icon, agency, subtext): #test user creation... # basically a User constructor using a given set of values # to save me some typing use = User() name = name.replace(' ', '_') use.setProfileInfo(name, icon, agency, subtext) return use
def test_userPurchaseDeductsBalance(self): user = User() user.resources.science._value = 100 bal = Balance(science=Cost(-10,0)) assert(user.affords(bal)) user.purchase(balance=bal) val = user.resources.science._value self.assertEqual(val,90)
def test_track_several_asteroids_with_lots_of_funds_is_success(self): user = User("test user") user.setGame(Game()) LARGE_NUMBER = 1000000000 # make user rich! user.resources.applyBalance( Balance(Cost(LARGE_NUMBER), Cost(LARGE_NUMBER), Cost(LARGE_NUMBER), Cost(LARGE_NUMBER), Cost(LARGE_NUMBER)) ) fake_asteroid = "{'orbitName':'test_orbitName', 'objectId':'test_objectID'}" n_tests = 5 for i in range(n_tests): self.assertEqual(tracker.asteroid_track_request_responder(fake_asteroid, user), tracker.RESPONSE_SUCCESS)
def login_success(): token = request.forms.get('token') session = rauth.OAuth2Session(config.GOOGLE_CLIENT_ID, config.GOOGLE_CLIENT_SECRET, token) json = session.get('https://www.googleapis.com/oauth2/v1/userinfo').json() if json is None: return def convert(inputt): if isinstance(inputt, dict): return {convert(key): convert(value) for key, value in inputt.iteritems()} elif isinstance(inputt, list): return [convert(element) for element in inputt] elif isinstance(inputt, unicode): return inputt.encode('utf-8') else: return input json = convert(json) name = json['name'].replace(" ", "_") user = User(name, json['picture'], "NASA", "For the Benefit of All") gameToken = user.name gameToken = gameToken.replace(" ", "_") USERS.addUser(user, gameToken) response.set_cookie("cosmosium_login", gameToken, max_age=60 * 60 * 5) loginTokens.append({'name': user.name, 'social_token': token, 'game_token': gameToken}) # now that we're logged in, send the user where they were trying to go, else to main page target = request.query.target or '/play' redirect(target)
def test_track_several_asteroids_with_lots_of_funds_is_success(self): user = User('test user') user.setGame(Game()) LARGE_NUMBER = 1000000000 # make user rich! user.resources.applyBalance( Balance(Cost(LARGE_NUMBER), Cost(LARGE_NUMBER), Cost(LARGE_NUMBER), Cost(LARGE_NUMBER), Cost(LARGE_NUMBER))) fake_asteroid = "{'orbitName':'test_orbitName', 'objectId':'test_objectID'}" n_tests = 5 for i in range(n_tests): self.assertEqual( tracker.asteroid_track_request_responder(fake_asteroid, user), tracker.RESPONSE_SUCCESS)
def createUser(name, icon, agency, subtext): use = User() use.setProfileInfo(name, icon, agency, subtext) return use
def setLoginCookie(): uid = request.forms.get("userid") pw = request.forms.get("password") rem = request.forms.get("remember_me") if client is not None: db = client.users hash_pass = SHA256.new(pw).digest() # hash user input password hash_pass_uni = hash_pass.decode("latin-1") # convert to unicode salt = db.test_user.find_one({"user": uid}, {"salt": 1, "_id": 0}) # pull user salt salt_uni = salt["salt"] # pull single salt unicode element password = db.test_user.find_one({"user": uid}, {"password": 1, "_id": 0}) # pull user password password_uni = password["password"] # pull single password unicode element if str(db.test_user.find_one({"user": uid})) == "None": return "User not Found" else: if password_uni == salt_uni + hash_pass_uni: # matches input password to db password _user = USERS.getUserByName(uid) # TODO: replace this with db lookup loginToken = ( uid + "loginToken" + "".join(random.choice(string.ascii_letters + string.digits) for _ in range(5)) ) def createUser(name, icon, agency, subtext): use = User() use.setProfileInfo(name, icon, agency, subtext) return use mongo_user = db.test_user.find_one({"user": uid}, {"user": 1, "_id": 0})[ "user" ] # pull out mongodb query and display only the value of the approriate key, i.e. pymongo returns a <type 'dict'> mongo_org = db.test_user.find_one({"user": uid}, {"org": 1, "_id": 0})["org"] mongo_quote = db.test_user.find_one({"user": uid}, {"quote": 1, "_id": 0})["quote"] userObj = createUser(mongo_user, "/img/profiles/martin2.png", mongo_org, mongo_quote) try: USERS.addUser(userObj, loginToken) except ValueError as e: print e.message response.set_cookie("cosmosium_login", loginToken, max_age=60 * 60 * 5) redirect("/play") else: return "Wrong Password" else: offlineUser = User() offlineUser.setProfileInfo("OfflineUser", "/img/profiles/martin2.png", "Offline", "Offline") loginToken = ( "OfflineUser" + "loginToken" + "".join(random.choice(string.ascii_letters + string.digits) for _ in range(5)) ) try: USERS.addUser(offlineUser, loginToken) except ValueError as e: print e.message response.set_cookie("cosmosium_login", loginToken, max_age=60 * 60 * 5) redirect("/play")
def createUser(name, icon, agency, subtext ): # basically a User constructor using a given set of values # to save me some typing use = User() use.setProfileInfo(name,icon,agency,subtext) return use
def test_deltaResourcesGoesUp_if_researchAgeIncreases(self): user = User()
def test_userCannotAffordValue_returns_false(self): user = User() bal = Balance(science=Cost(-10,0)) assert(user.resources.science._value+bal.science.oneTime<=0) self.assertFalse(user.purchase(balance=bal))
def createUser(name, icon, agency, subtext): # basically a User constructor using a given set of values # to save me some typing use = User() use.setProfileInfo(name, icon, agency, subtext) return use