def setUpClass(self):

        self.app = create_app(testing=True)
        self.app_context = self.app.app_context()
        self.app_context.push()
        self.client = self.app.test_client()

        LOG.info("Initializing tests.")

        #Create a brand new test db
        db.create_all()

        #Add a clientid and apikey
        new_client = Client("myclientid", "myapikey", "normal")
        new_admin_client = Client("dashboard", "dashboardapikey", "admin")
        db.session.add(new_client)
        db.session.add(new_admin_client)
        try:
            db.session.commit()
            LOG.info("=== Added clients ===")
        except Exception as e:
            LOG.error(e, exc_info=True)

        #Generating gaming sessions ids
        self.newsessionid = UUID(bytes=OpenSSL.rand.bytes(16)).hex
        self.newsessionid2 = UUID(bytes=OpenSSL.rand.bytes(16)).hex
        self.newsessionid3 = UUID(
            bytes=OpenSSL.rand.bytes(16)).hex  #session not in db
        self.unauthorized_sessionid = "ac52bb1d811356ab3a8e8711c5f7ac5d"

        new_session = Session(self.newsessionid, new_client.id)
        new_session2 = Session(self.newsessionid2, new_client.id)
        db.session.add(new_session)
        db.session.add(new_session2)
        try:
            db.session.commit()
            LOG.info("=== Added sessions ===")
            LOG.info("=== Session not in db: %s ===" % self.newsessionid3)
        except Exception as e:
            LOG.error(e, exc_info=True)

        #Generating tokens
        self.mytoken = new_client.generate_auth_token(self.newsessionid)
        self.myexpiredtoken = new_client.generate_auth_token(self.newsessionid,
                                                             expiration=1)

        self.mytokennewsession = new_client.generate_auth_token(
            self.newsessionid3)

        self.myadmintoken = new_admin_client.generate_auth_token()
        self.myexpiredadmintoken = new_admin_client.generate_auth_token(
            expiration=1)

        self.mybadtoken = "badlogin" + self.mytoken.decode()[8:]
        self.mybadtoken = self.mybadtoken.encode("ascii")

        self.xml_valid_event = """<event><timestamp>2015-11-29T12:10:57Z</timestamp>
        <action>STARTGAME</action><level></level><update></update><which_lix>
        </which_lix><result></result></event>"""
        self.json_valid_event = """[{
                "timestamp": "2015-11-29T12:10:57Z",
                "action": "STARTGAME",
                "which_lix": ""
              }]"""
        self.xml_invalid_event = """<event>a
         <action>STARTGAME</action>
         <timestamp>2015-11-29T12:10:57Z</timestamp>
         <which_lix />
      </event>"""
        self.json_invalid_event = """
                "timestamp": "2015-11-29T12:10:57Z",
                "action": "STARTGAME",,
                "which_lix": ""
              """
        self.xml_multiple_events = """<event>
         <action>STARTGAME</action>
         <timestamp>2015-11-29T12:10:57Z</timestamp>
         <which_lix />
      </event>
      <event>
         <action>ENDGAME</action>
         <timestamp>2015-11-29T13:10:57Z</timestamp>
         <which_lix />
      </event>"""

        self.json_multiple_events = """[{ "timestamp": "2015-11-29T12:10:57Z",
                "action": "STARTGAME",
                "which_lix": ""
              }, {
                "timestamp": "2015-11-29T13:10:57Z",
                "action": "ENDGAME",
                "which_lix": ""
              }]"""

        time.sleep(3)  #expire the token

        new_gameevent = GameEvent(new_session.id, self.xml_valid_event)
        db.session.add(new_gameevent)
        try:
            db.session.commit()
            LOG.info("=== Added game event. All set up. ===")
        except Exception as e:
            LOG.error(e, exc_info=True)
 def setUpClass(self):
     
     self.app = create_app(testing=True)
     self.app_context = self.app.app_context()
     self.app_context.push()
     self.client = self.app.test_client()
     
     LOG.info("Initializing tests.")
     
     #Create a brand new test db
     db.create_all()
     
     #Add a clientid and apikey
     new_client = Client("myclientid", "myapikey", "normal")
     new_admin_client = Client("dashboard", "dashboardapikey", "admin")  
     db.session.add(new_client)
     db.session.add(new_admin_client)
     try:
         db.session.commit()
         LOG.info("=== Added clients ===")
     except Exception as e:
         LOG.error(e, exc_info=True)      
     
     #Generating gaming sessions ids
     self.newsessionid = UUID(bytes = OpenSSL.rand.bytes(16)).hex
     self.newsessionid2 = UUID(bytes = OpenSSL.rand.bytes(16)).hex
     self.newsessionid3 = UUID(bytes = OpenSSL.rand.bytes(16)).hex #session not in db
     self.unauthorized_sessionid = "ac52bb1d811356ab3a8e8711c5f7ac5d"
     
     new_session = Session(self.newsessionid, new_client.id)
     new_session2 = Session(self.newsessionid2, new_client.id)
     db.session.add(new_session)
     db.session.add(new_session2)
     try:
         db.session.commit()
         LOG.info("=== Added sessions ===")
         LOG.info("=== Session not in db: %s ===" % self.newsessionid3)
     except Exception as e:
         LOG.error(e, exc_info=True)
     
     #Generating tokens        
     self.mytoken = new_client.generate_auth_token(self.newsessionid)
     self.myexpiredtoken = new_client.generate_auth_token(self.newsessionid, expiration=1)
     
     self.mytokennewsession = new_client.generate_auth_token(self.newsessionid3)
     
     self.myadmintoken = new_admin_client.generate_auth_token()
     self.myexpiredadmintoken = new_admin_client.generate_auth_token(expiration=1)
     
     self.mybadtoken = "badlogin" + self.mytoken.decode()[8:]
     self.mybadtoken = self.mybadtoken.encode("ascii")
     
     self.xml_valid_event = """<event><timestamp>2015-11-29T12:10:57Z</timestamp>
     <action>STARTGAME</action><level></level><update></update><which_lix>
     </which_lix><result></result></event>""";
     self.json_valid_event = """[{
             "timestamp": "2015-11-29T12:10:57Z",
             "action": "STARTGAME",
             "which_lix": ""
           }]"""
     self.xml_invalid_event = """<event>a
      <action>STARTGAME</action>
      <timestamp>2015-11-29T12:10:57Z</timestamp>
      <which_lix />
   </event>"""
     self.json_invalid_event = """
             "timestamp": "2015-11-29T12:10:57Z",
             "action": "STARTGAME",,
             "which_lix": ""
           """
     self.xml_multiple_events = """<event>
      <action>STARTGAME</action>
      <timestamp>2015-11-29T12:10:57Z</timestamp>
      <which_lix />
   </event>
   <event>
      <action>ENDGAME</action>
      <timestamp>2015-11-29T13:10:57Z</timestamp>
      <which_lix />
   </event>"""
     
     self.json_multiple_events = """[{ "timestamp": "2015-11-29T12:10:57Z",
             "action": "STARTGAME",
             "which_lix": ""
           }, {
             "timestamp": "2015-11-29T13:10:57Z",
             "action": "ENDGAME",
             "which_lix": ""
           }]"""
     
     time.sleep(3) #expire the token
  
     new_gameevent = GameEvent(new_session.id,self.xml_valid_event)
     db.session.add(new_gameevent)        
     try:
         db.session.commit()
         LOG.info("=== Added game event. All set up. ===")
     except Exception as e:
         LOG.error(e, exc_info=True)
 def tearDownClass(self):
     LOG.info("======================Finished tests====================")
     db.session.remove()
     db.drop_all()
     self.app_context.pop()
 def tearDownClass(self):
     LOG.info("======================Finished tests====================")
     db.session.remove()
     db.drop_all()
     self.app_context.pop()