test_flask.py 6.17 KB
import os, time, requests, json, threading
from database import DBconnection

URL = "http://192.168.15.21:5000/"
TESTPHONE = "5493415959169"
TESTMAIL = "sujetodeprueba0110@gmail.com"
FOLDER = "testfolder/"
SENDING_TIME = 60 # aumentar a medida que se vuelva necesario
START_TIME = 10 # cambiar dependiendo de la velocidad de la máquina

def wpp1(phone):
  id = requests.post(url = URL + "data", files = {
    'wpmsg' : open(FOLDER + "wptext","rb"),
    'wpimage' : open(FOLDER + "wpimage","rb"),
    'wpmedia' : open(FOLDER + "wpmedia","rb"),
    'wplink' : open(FOLDER + "wplink","rb")
  })
  assert int(id.text) > 0 , id.text + "no es mayor a 0"
  state = requests.post(url = URL + "msg", params = {
    'id' : id.text,
    'serv' : "wpp1",
    'dest' : phone,
    'type' : json.dumps({
      'wpmsg' : 'text',
      'wpimage' : 'image',
      'wpmedia' : 'document',
      'wplink' : 'link'
    }),
    'conf' : 'true'
  })
  assert state.text == "queued" , "'" + state.text + "' no es igual a 'queued'"
  time.sleep(SENDING_TIME) 
  state = requests.post(url = URL + "cons", params = {
    'id' : id.text
  })
  assert (state.text == "delivered") , "'" + state.text + "' no es igual a 'delivered'"

def mail(mail):
  id = requests.post(url = URL + "data", files = {
    'mailmsg' : open(FOLDER + "mailtext","rb"),
    'mailimage' : open(FOLDER + "mailimage","rb"),
    'maildocument' : open(FOLDER + "maildocument","rb")
  })
  assert int(id.text) > 0 , id.text + "no es mayor a 0"
  state = requests.post(url = URL + "msg", params = {
    'id' : id.text,
    'serv' : "mail",
    'dest' : mail,
    'type' : json.dumps({
      'mailmsg' : 'text',
      'mailimage' : 'image',
      'maildocument' : 'document'
    }),
    'info' : json.dumps({
      'subject' : 'Some Subject'
    }),
    'conf' : 'true'
  })
  assert state.text == "queued" , "'" + state.text + "' no es igual a 'queued'"
  time.sleep(SENDING_TIME) 
  state = requests.post(url = URL + "cons", params = {
    'id' : id.text
  })
  assert (state.text == "delivered") , "'" + state.text + "' no es igual a 'delivered'"

def encryption(phone):
  key = requests.get(url = URL + "key")
  f = open(FOLDER + "server_rsa_key.pub", "+w")
  f.write(key.text)
  f.close()
  os.system("openssl rand -base64 32 > " + FOLDER + "rand.key")
  os.system("openssl enc -aes-256-cbc -salt -in " + FOLDER + "plaintext" " -out " + FOLDER + "plaintext.enc -pass file:" + FOLDER + "rand.key")
  os.system("openssl enc -aes-256-cbc -salt -in " + FOLDER + "plainimage" " -out " + FOLDER + "plainimage.enc -pass file:" + FOLDER + "rand.key")
  os.system("openssl rsautl -encrypt -inkey " + FOLDER + "server_rsa_key.pub -pubin -in " + FOLDER + "rand.key -out " + FOLDER + "rand.key.enc")
  
  id = requests.post(url = URL + "data", files = {
    'plaintext' : open(FOLDER + "plaintext.enc","rb"),
    'plainimage' : open(FOLDER + "plainimage.enc","rb"),
    'key' : open(FOLDER + "rand.key.enc","rb")
  })
  assert int(id.text) > 0 , id.text + "no es mayor a 0"
  state = requests.post(url = URL + "msg", params = {
    'id' : id.text,
    'serv' : "wpp1",
    'dest' : phone,
    'type' : json.dumps({
      'plaintext' : 'text',
      'plainimage' : 'image'
    }),
    'conf' : 'true'
  })
  assert state.text == "queued" , "'" + state.text + "' no es igual a 'queued'"
  time.sleep(SENDING_TIME) 
  state = requests.post(url = URL + "cons", params = {
    'id' : id.text
  })
  assert (state.text == "delivered") , "'" + state.text + "' no es igual a 'delivered'"

def errors(phone):
  # id no existente
  error = requests.post(url = URL + "msg", params = {
    'id' : "-",
    'serv' : "-",
    'dest' : "-",
    'type' : "-"
  })
  assert error.text == "El id - no existe"
  # servicio no valido
  id = requests.post(url = URL + "data", files = {
    "errortext" : open(FOLDER + "errortext","rb")
  })
  error = requests.post(url = URL + "msg", params = {
    'id' : id.text,
    'serv' : "-",
    'dest' : "-",
    'type' : "-"
  })
  assert error.text == "No existe el servicio '-'"
  # archivo inexistente
  error = requests.post(url = URL + "msg", params = {
    'id' : id.text,
    'serv' : "wpp1",
    'dest' : phone,
    'type' : json.dumps({
      "-" : "-"
    })
  })
  assert error.text == "El archivo '-' no existe"
  # tipo incorrecto
  error = requests.post(url = URL + "msg", params = {
    'id' : id.text,
    'serv' : "wpp1",
    'dest' : phone,
    'type' : json.dumps({
      "errortext" : "-"
    })
  })
  assert error.text == "El servicio 'wpp1' no puede enviar el tipo '-' destinado al archivo 'errortext'"
  # parámetro incorrecto
  error = requests.post(url = URL + "msg", params = {
    'id' : id.text,
    'serv' : "wpp1",
    'dest' : phone,
    'type' : json.dumps({
      "errortext" : "text"
    }),
    'info' : json.dumps({
      "-" : "-"
    })
  })
  assert error.text == "El servicio 'wpp1' no cuenta con algún parámetro enviado" 

def noconf(phone):
  id = requests.post(url = URL + "data", files = {
    'wpmsg' : open(FOLDER + "wptext","rb")
  })
  assert int(id.text) > 0 , id.text + "no es mayor a 0"
  state = requests.post(url = URL + "msg", params = {
    'id' : id.text,
    'serv' : "wpp1",
    'dest' : phone,
    'type' : json.dumps({
      'wpmsg' : 'text'
    })
  })
  assert state.text == "queued" , "'" + state.text + "' no es igual a 'queued'"
  time.sleep(SENDING_TIME) 
  error = requests.post(url = URL + "cons", params = {
    'id' : id.text
  })
  assert (error.text == "El id " + id.text + " no existe") , "'" + error.text + "' no es igual a 'El id " + id.text + " no existe'"
  
def main():

  try:
    os.system("python3 deploy.py &")
    time.sleep(START_TIME)
  
    threads = []
    threads.append(threading.Thread(target = wpp1, args = (TESTPHONE,)))
    threads.append(threading.Thread(target = encryption, args = (TESTPHONE,)))
    threads.append(threading.Thread(target = mail, args = (TESTMAIL,)))
    threads.append(threading.Thread(target = errors, args = (TESTPHONE,)))
    threads.append(threading.Thread(target = noconf, args = (TESTPHONE,)))

    for thread in threads:
      thread.start()

    for thread in threads:
      thread.join()

  except Exception as exp:
    print(type(exp))
    print(exp.args)
  finally:
    os.system("killall python3")


if __name__ == "__main__":
  main()