def setUp(self): meep_example_app.initialize() app = meep_example_app.MeepExampleApp() self.app = app u = meeplib.User('foo', 'bar', -1) v = meeplib.User('foo2', 'bar2', -1) m = meeplib.Message('my title', 'lol', u, -1)
def testAddUser_newportt(self): print "test add user" meeplib.User("cait", "test") meeplib.User('new', 'test') for user in meeplib.get_all_users(): print user.username assert len(meeplib._users) == 3 meeplib._reset()
def initialize(): try: meeplib._messages = {} meeplib._users = {} meeplib._user_ids = {} meeplib._openMeep() except IOError: # create default users u = meeplib.User('test', 'foo') a = meeplib.User('Anonymous', 'password') meeplib.Message('my title', 'This is my message!', u, -1)
def setUp(self): x = meeplib.get_all_messages() print "Messages",x for message in x: meeplib.delete_message(message) x = meeplib.get_all_messages() print "Messages",x u = meeplib.User('foo', 'bar',-1) v = meeplib.User('foo2', 'bar2',-1) m = meeplib.Message('the title', 'the content', u,-1)
def test_get_next_user_id(self): #there should be 1 user initially assert meeplib._get_next_user_id() == 1 user = meeplib.User('admin', 'admin') user.insertIntoDB() #there should be 2 users now assert meeplib._get_next_user_id() == 2
def test_list_messages(self): environ = {} # make a fake dict environ['PATH_INFO'] = '/m/list' u = meeplib.User('user', 'name') m = meeplib.Message('init title', 'init message', u, '!') def fake_start_response(status, headers): assert status == '200 OK' assert ('Content-type', 'text/html') in headers data = self.app(environ, fake_start_response) index = 0 for m in data: if "Title: init title" in m: index += 1 if 'Message: init message' in m: index += 1 if 'Author: user' in m: index += 1 if 'ID: 0' in m: index += 1 print index assert index is 4
def initialize(): # create a default user u = meeplib.User('test', 'foo') # create a single message meeplib.Message('my title', 'This is my message!', u) meeplib.load()
def test_main_page(self): environ = {} # make a fake dict environ['PATH_INFO'] = '/main_page' u = meeplib.User('user', 'name') meeplib.set_curr_user(u.username) m = meeplib.Message('init title', 'init message', u, '!') def fake_start_response(status, headers): assert status == '200 OK' assert ('Content-type', 'text/html') in headers data = self.app(environ, fake_start_response) index = 0 for m in data: if "Add Message" in m: index += 1 if 'Create User' in m: index += 1 if 'Logout' in m: index += 1 if 'Show Messages' in m: index += 1 #print m assert index is 4
def add_new_user(self, environ, start_response): print environ['wsgi.input'] form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ) returnStatement = "user added" try: username = form['username'].value except KeyError: username = None try: password = form['password'].value except KeyError: password = None print username print password # Test whether variable is defined to be None if username is None: returnStatement = "username was not set. User could not be created" if password is None: returnStatement = "password was not set. User could not be created" else: new_user = meeplib.User(username, password) meeplib.save() headers = [('Content-type', 'text/html')] headers.append(('Location', '/')) start_response("302 Found", headers) return [returnStatement]
def initialize(): print "enter initialize" meeplib.load_state() u = meeplib.User('test', 'foo') t = meeplib.Thread('Greetings Earthlings') m = meeplib.Message('The meep message board is open.', u) t.add_post(m)
def create_user(self, environ, start_response): # get cookie if there is one try: cookie = Cookie.SimpleCookie(environ["HTTP_COOKIE"]) username = cookie["username"].value except: username = '' user = meeplib.get_user(username) if user is not None: headers = [('Content-type', 'text/html')] headers.append(('Location', '/')) start_response("302 Found", headers) return ["You must be logged out to use that feature."] headers = [('Content-type', 'text/html')] print environ['wsgi.input'] form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ) try: username = form['username'].value except KeyError: username = '' try: password = form['password'].value except KeyError: password = '' s = [] ##if we have username and password if username != '': user = meeplib.get_user(username) ## user already exists if user is not None: s.append('''Creation Failed! <br> User already exists, please use a different username.<p>''' ) ## user doesn't exist but they messed up the passwords elif password == '': s.append('''Creation Failed! <br> Please fill in the Password field<p>''') else: u = meeplib.User(username, password) meeplib.save_state() ## send back a redirect to '/' k = 'Location' v = '/' headers.append((k, v)) cookie_name, cookie_val = meepcookie.make_set_cookie_header( 'username', username) headers.append((cookie_name, cookie_val)) start_response('302 Found', headers) s.append(render_page("create_user.html", username=username)) return [''.join(s)]
def setUp(self): self.ux = 'foo' self.px = 'bar' self.msgTitle = 'the title' self.msgMsg = 'the content' u = meeplib.User(self.ux, self.px) m = meeplib.Message(self.msgTitle, self.msgMsg, u) self.msgId = m.id
def test_delete(self): u = meeplib.User('foo', 'bar') m = meeplib.Message('my title', 'This is my message!', u) assert m in meeplib._messages.values() meeplib.delete_message(m) assert m not in meeplib._messages.values()
def initialize(): # create a default user u = meeplib.User('aLlama', 'box') # create a thread (i.e. title for message) t = meeplib.Thread('This Is A Test Thread') # create a single message m = meeplib.Message('This is a lame test message.', u) # save the message in the thread t.add_post(m)
def initialize(): # create a default user u = meeplib.User('test', 'foo') # create a thread t = meeplib.Thread('Test Thread') # create a single message m = meeplib.Message('This is my message!', u) # save the message in the thread t.add_post(m)
def initialize(): try: meeplib._load_data() except IOError: # file does not exist/cannot be opened # initialize data from scratch # create a default user with username: test, password: test u = meeplib.User('test', 'test') # create a single message and topic meeplib.Topic('First Topic', meeplib.Message('my title', 'This is my message!', u), u)
def test_get_next_message_id(self): #there should be 1 message initially created assert meeplib._get_next_message_id() == 1 user = meeplib.User('admin', 'admin') user.insertIntoDB() author = meeplib.get_user('admin') message = meeplib.Message('title', 'message', author.id, -1) message.insertIntoDB() #there should not be two messages assert meeplib._get_next_message_id() == 2
def test_recursive_delete(self): u = meeplib.User('foo', 'bar') m = meeplib.Message('the title', 'the content', u, '!') n = meeplib.Message('the reply title', 'the reply', u, m.id) o = meeplib.Message('the second tier title', 'the second reply', u, n.id) meeplib.delete_message(m) assert n not in meeplib._messages.values() assert o not in meeplib._messages.values()
def login(self, environ, start_response): print environ['wsgi.input'] form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ) returnStatement = "logged in" try: username = form['username'].value except KeyError: username = None try: password = form['password'].value except KeyError: password = None k = '' v = '' print "USERNAME: "******"!" # Test whether variable is defined to be None if username is not None: if password is not None: if meeplib.check_user(username, password) is False: k = 'Location' v = '/' returnStatement = """<p>Invalid user. Please try again.</p>""" else: new_user = meeplib.User(username, password) meeplib.set_curr_user(username) k = 'Location' v = '/main_page' else: returnStatement = """<p>password was not set. User could not be created</p>""" else: returnStatement = """<p>username was not set. User could not be created</p>""" #print """isValidafter: %s """ %(meeplib.check_user(username, password),) # set content-type headers = [('Content-type', 'text/html')] if returnStatement is "logged in": c = SimpleCookie() cookie_name, cookie_val = make_set_cookie_header( 'username', username) headers.append((cookie_name, cookie_val)) print cookie_name + cookie_val headers.append((k, v)) start_response('302 Found', headers) #return self.main_page(environ, start_response) return returnStatement
def initialize(): #clear everything out before loading, this fixes a few tests meeplib._messages = {} meeplib._users = {} meeplib._user_ids = {} meeplib._load_backup() if (len(meeplib._messages) == 0 and len(meeplib._users) == 0): # create default users u = meeplib.User('test', 'foo') u.insertIntoDB() a = meeplib.User('Anonymous', 'password') a.insertIntoDB() x = meeplib.User('studentx', 'passwordx') x.insertIntoDB() y = meeplib.User('studenty', 'passwordy') y.insertIntoDB() z = meeplib.User('studentz', 'passwordz') z.insertIntoDB() m = meeplib.Message('my title', 'This is my message!', u.id, -1) m.insertIntoDB()
def login(self, environ, start_response): try: cookie = Cookie.SimpleCookie(environ["HTTP_COOKIE"]) username = cookie["username"].value #print "Username = %s" % username except: #print "session cookie not set! defaulting username" username = '' print environ['wsgi.input'] form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ) returnStatement = "logged in" try: username = form['username'].value except KeyError: username = None try: password = form['password'].value except KeyError: password = None k = 'Location' v = '/' # set content-type headers = [('Content-type', 'text/html')] # Test whether variable is defined to be None if username is not None: if password is not None: if meeplib.check_user(username, password) is True: new_user = meeplib.User(username, password) meeplib.set_curr_user(username) # set the cookie to the username string cookie_name, cookie_val = meepcookie.make_set_cookie_header( 'username', username) headers.append((cookie_name, cookie_val)) else: returnStatement = """<p>Invalid user. Please try again.</p>""" else: returnStatement = """<p>password was not set. User could not be created</p>""" else: returnStatement = """<p>username was not set. User could not be created</p>""" print """isValidafter: %s """ % (meeplib.check_user( username, password), ) headers.append((k, v)) start_response('302 Found', headers) return "no such content"
def setUp(self): #the backup data causes some of the tests to fail - not sure why #remove the backup data before every test cur.execute("DELETE FROM MESSAGE") cur.execute("DELETE FROM SESSION") cur.execute("DELETE FROM USER") meeplib._reset() con.commit() u = meeplib.User('foo', 'bar') u.insertIntoDB() m = meeplib.Message('the title', 'the content', u.id, -1) m.insertIntoDB()
def create_user(self, environ, start_response): headers = [('Content-type', 'text/html')] post = (environ.get('REQUEST_METHOD') == 'POST') if post: form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ) else: form = parse_qs(environ['QUERY_STRING']) username = self.get_value(form, post, 'username', '') password = self.get_value(form, post, 'password', '') password2 = self.get_value(form, post, 'password_confirm', '') s = [] ##if we have username and password and confirmation password err = False if username != '': user = meeplib.get_user(username) ## user already exists if user is not None: print 'nouser' err = True ## user doesn't exist but they messed up the passwords elif password == '': print 'passblank' err = True elif password != password2: print 'pass2blank' err = True else: print 'else' u = meeplib.User(username, password) ## send back a redirect to '/' k = 'Location' v = '/' headers.append((k, v)) self.log_user_in(environ, headers, u.username) start_response('302 Found', headers) return [] elif password != '' or password2 != '': err = True if post: err = True headers = [('Content-type', 'text/html')] start_response('200 OK', headers) return [render_page('createUser.html', error=err)]
def test_get_root_messages(self): user = meeplib.User('admin', 'admin') user.insertIntoDB() author = meeplib.get_user('admin') message1 = meeplib.Message('title1', 'message1', author.id, -1) message1.insertIntoDB() message2 = meeplib.Message('title2', 'message2', author.id, -1) message2.insertIntoDB() message3 = meeplib.Message('title3', 'message3', author.id, 0) message3.insertIntoDB() root_messages = meeplib._get_root_messages() assert message1 in root_messages assert message2 in root_messages assert message3 not in root_messages
def login(self, environ, start_response): try: cookie = Cookie.SimpleCookie(environ["HTTP_COOKIE"]) username = cookie["username"].value except: username = '' print environ['wsgi.input'] form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ) returnStatement = "Logged In" try: username = form['username'].value except KeyError: username = None try: password = form['password'].value except KeyError: password = None k = 'Location' v = '/' headers = [('Content-type', 'text/html')] # Check to see if defined as None if username is not None: if password is not None: if meeplib.check_user(username, password) is True: new_user = meeplib.User(username, password) meeplib.set_curr_user(username) #set the cookie to the username cookie_name, cookie_val = meepcookie.make_set_cookie_header('username',username) headers.append((cookie_name, cookie_val)) else: returnStatement = """<p>You entered a Higgs boson user. You do not exist(yet). Please try again.</p>""" else: returnStatement = """<p>Password was not set. User was not created.</p>""" else: returnStatement = """<p>Username was not set. User was not created</p>""" print """isValidafter: %s """ %(meeplib.check_user(username, password),) headers.append((k, v)) start_response('302 Found', headers) return "The content is a Higgs boson."
def create_user(self, environ, start_response): print "enter create user" username = check_cookie(environ) user = meeplib.get_user(username) if user is not None: headers = [('Content-type', 'text/html')] headers.append(('Location', '/')) start_response("302 Found", headers) return ["log out to use that feature"] headers = [('Content-type', 'text/html')] form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ) try: username = form['username'].value except KeyError: username = '' try: password = form['password'].value except KeyError: password = '' s = [] ##if we have username and password if username != '': user = meeplib.get_user(username) ## user already exists if user is not None: s.append('''user already exists; <br> please use a different username.<p>''') ## user doesn't exist but they messed up the passwords elif password == '': s.append('''enter a password <br/>''') else: u = meeplib.User(username, password) meeplib.save_state() ## send back a redirect to '/' k = 'Location' v = '/' headers.append((k, v)) cookie_name, cookie_val = meepcookie.make_set_cookie_header( 'username', username) headers.append((cookie_name, cookie_val)) start_response('302 Found', headers) s.append(render_page("create_user.html", username=username)) print "exit create user" return [''.join(s)]
def initialize(): # create a default user #u = meeplib.User('test', 'foo') # create a single message #meeplib.Message('my title', 'This is my message!', u, "!") try: fp = open('users.pickle') try: obj = pickle.load(fp) while True: #print obj (a, b) = obj u = meeplib.User(a, b) try: obj = pickle.load(fp) except EOFError: break except EOFError: pass except IOError: fp = open('users.pickle', "w") fp.close() try: fp = open('messages.pickle') try: obj = pickle.load(fp) while True: (a, b, c, d, e) = obj #print obj m = meeplib.Message(a, b, meeplib.get_user(c), d) m.id = e #print m.post + " is " + str(m.id) #obj = pickle.load(fp) #m.replies = obj #print obj try: obj = pickle.load(fp) except EOFError: break except EOFError: pass except IOError: p = open('messages.pickle', "w") fp.close()
def create_user_action(self, environ, start_response): print environ['wsgi.input'] form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ) #TODO Error Checking on Creating a User returnStatement = "user added" username = form['username'].value password = form['password'].value new_user = meeplib.User(username, password) headers = [('Content-type', 'text/html')] headers.append(('Location', '/')) start_response("302 Found", headers) return [returnStatement]
def test_backup_and_load_meep(self): #create a user, and message, then backup, reset data in app meeplib.User('admin', 'admin') author = meeplib.get_user('admin') author.insertIntoDB() message = meeplib.Message('title', 'message', author.id, -1) message.insertIntoDB() meeplib._reset() #check that the file exists and contains information about the message and user try: meeplib._load_backup() assert author in meeplib._users.values() assert message in meeplib._messages.values() except: assert False #the test failed
def add_user_action(self, environ, start_response): #print environ['wsgi.input'] form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ, keep_blank_values=True) try: username = form['username'].value password = form['password'].value user = meeplib.User(username, password) except: pass headers = [('Content-type', 'text/html')] headers.append(('Location', '/')) start_response('302 Found', headers) return ["user added"]