Example #1
0
 def add_user(
     email: str,
     password: str,
     first_name: str,
     last_name: str,
     account: int,
     bot: str,
     user: str,
     is_integration_user=False,
     role="trainer",
 ):
     """ Adds a new user to the app based on the details
         provided by the user """
     assert not Utility.check_empty_string(email) and not Utility.check_empty_string(last_name) and not Utility.check_empty_string(first_name) and not Utility.check_empty_string(password),"Email, FirstName, LastName and password cannot be empty or blank spaces "
     Utility.is_exist(
         User,
         exp_message="User already exists! try with different email address.",
         email__iexact=email.strip(), status=True
     )
     return (
         User(
             email=email.strip(),
             password=Utility.get_password_hash(password.strip()),
             first_name=first_name.strip(),
             last_name=last_name.strip(),
             account=account,
             bot=bot.strip(),
             user=user.strip(),
             is_integration_user=is_integration_user,
             role=role.strip(),
         )
         .save()
         .to_mongo()
         .to_dict()
     )
Example #2
0
 def validate(self, clean=True):
     if not self.title or not self.payload:
         raise ValidationError("title and payload must be present!")
     elif Utility.check_empty_string(
             self.title) or Utility.check_empty_string(
                 self.payload.strip()):
         raise ValidationError(
             "Response title and payload cannot be empty or blank spaces")
Example #3
0
 def validate(self, clean=True):
     if (Utility.check_empty_string(self.email)
             or Utility.check_empty_string(self.first_name)
             or Utility.check_empty_string(self.last_name)
             or Utility.check_empty_string(self.password)):
         raise ValidationError(
             "Email, FirstName, LastName and password cannot be empty or blank space"
         )
     elif isinstance(email(self.email), ValidationFailure):
         raise ValidationError("Please enter valid email address")
Example #4
0
 def validate(self, clean=True):
     if Utility.check_empty_string(self.name) or Utility.check_empty_string(
             self.pattern):
         raise ValidationError(
             "Regex name and pattern cannot be empty or blank spaces")
     else:
         try:
             re.compile(self.pattern)
         except AppException as e:
             raise AppException("invalid regular expression " +
                                self.pattern)
Example #5
0
 def validate(self, clean=True):
     if (Utility.check_empty_string(self.type)
             or Utility.check_empty_string(self.url)
             or Utility.check_empty_string(self.db)):
         raise ValidationError(
             "Type, Url and DB cannot be blank or empty spaces")
     else:
         if self.type == "mongo":
             try:
                 parse_uri(self.url)
             except InvalidURI:
                 raise AppException("Invalid tracker url!")
Example #6
0
 def validate(self, clean=True):
     if self.entities:
         for ent in self.entities:
             ent.validate()
             extracted_ent = self.text[ent.start:ent.end]
             if extracted_ent != ent.value:
                 raise ValidationError(
                     "Invalid entity: " + ent.entity + ", value: " +
                     ent.value +
                     " does not match with the position in the text " +
                     extracted_ent)
     elif Utility.check_empty_string(
             self.text) or Utility.check_empty_string(self.intent):
         raise ValidationError(
             "Training Example name and text cannot be empty or blank spaces"
         )
Example #7
0
def test_augment_questions():
    responses.add(
        responses.POST,
        "http://localhost:8000/questions",
        json={
            "sucess": True,
            "data": {
                "questions": [
                    "where is digite centrally located?",
                    "where is digite conveniently located?",
                    "where is digite islocated?",
                    "where is digite situated?",
                    "where is digite strategically located?",
                ]
            },
            "message": None,
            "error_code": 0,
        },
        status=200,
    )
    response = client.post(
        "/api/augment/questions",
        headers={
            "Authorization": pytest.token_type + " " + pytest.access_token
        },
        json={"data": ["where is digite located?'"]},
    )

    actual = response.json()
    assert actual["success"]
    assert actual["error_code"] == 0
    assert actual["data"]
    assert Utility.check_empty_string(actual["message"])
Example #8
0
    async def get_current_user(self,
                               request: Request,
                               token: str = Depends(Utility.oauth2_scheme)):
        credentials_exception = HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Could not validate credentials",
            headers={"WWW-Authenticate": "Bearer"},
        )
        try:
            payload = jwt.decode(token,
                                 self.SECRET_KEY,
                                 algorithms=[self.ALGORITHM])
            username: str = payload.get("sub")
            if username is None:
                raise credentials_exception
            token_data = TokenData(username=username)
        except PyJWTError:
            raise credentials_exception
        user = AccountProcessor.get_user_details(token_data.username)
        if user is None:
            raise credentials_exception

        user_model = User(**user)
        if user['is_integration_user']:
            alias_user = request.headers.get("X-USER")
            if Utility.check_empty_string(alias_user):
                raise HTTPException(
                    status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
                    detail="Alias user missing for integration",
                    headers={"WWW-Authenticate": "Bearer"},
                )
            user_model.alias_user = alias_user
        return User(**user)
Example #9
0
    def __check_event_existence(self,
                                events: List[Dict],
                                bot: Text,
                                exp_message: Text = None,
                                raise_error=True):
        saved_events = list(
            Stories.objects(bot=bot, status=True).aggregate([{
                "$group": {
                    "_id": "$name",
                    "events": {
                        "$push": "$events"
                    }
                }
            }]))

        saved_items = list(
            itertools.chain.from_iterable(
                [items["events"] for items in saved_events]))

        if events in saved_items:
            if raise_error:
                if Utility.check_empty_string(exp_message):
                    raise AppException("Exception message cannot be empty")
                raise AppException(exp_message)
            else:
                return True
        else:
            if not raise_error:
                return False
Example #10
0
 def test_load_from_path_all_sccenario(self):
     processor = MongoProcessor()
     processor.save_from_path("tests/testing_data/all", "all", "testUser")
     training_data = processor.load_nlu("all")
     assert isinstance(training_data, TrainingData)
     assert training_data.training_examples.__len__() == 283
     assert training_data.entity_synonyms.__len__() == 3
     assert training_data.regex_features.__len__() == 5
     assert training_data.lookup_tables.__len__() == 1
     story_graph = processor.load_stories("all")
     assert isinstance(story_graph, StoryGraph) == True
     assert story_graph.story_steps.__len__() == 13
     domain = processor.load_domain("all")
     assert isinstance(domain, Domain)
     assert domain.slots.__len__() == 8
     assert domain.templates.keys().__len__() == 21
     assert domain.entities.__len__() == 7
     assert domain.form_names.__len__() == 2
     assert domain.user_actions.__len__() == 32
     assert domain.intents.__len__() == 22
     assert not Utility.check_empty_string(
         domain.templates["utter_cheer_up"][0]["image"]
     )
     assert domain.templates["utter_did_that_help"][0]["buttons"].__len__() == 2
     assert domain.templates["utter_offer_help"][0]["custom"]
     assert domain.slots[0].type_name == "unfeaturized"
Example #11
0
 def add_account(name: str, user: str):
     """ Adds a new account for the trainer app """
     assert not Utility.check_empty_string(name), "Account Name cannot be empty or blank spaces"
     Utility.is_exist(
         Account, exp_message="Account name already exists!", name__iexact=name, status=True
     )
     return Account(name=name.strip(), user=user).save().to_mongo().to_dict()
Example #12
0
    def __check_response_existence(self,
                                   response: Dict,
                                   bot: Text,
                                   exp_message: Text = None,
                                   raise_error=True):
        saved_responses = list(
            Responses.objects(bot=bot, status=True).aggregate([{
                "$group": {
                    "_id": "$name",
                    "texts": {
                        "$push": "$text"
                    },
                    "customs": {
                        "$push": "$custom"
                    },
                }
            }]))

        saved_items = list(
            itertools.chain.from_iterable([
                items["texts"] + items["customs"] for items in saved_responses
            ]))

        if response in saved_items:
            if raise_error:
                if Utility.check_empty_string(exp_message):
                    raise AppException("Exception message cannot be empty")
                raise AppException(exp_message)
            else:
                return True
        else:
            if not raise_error:
                return False
Example #13
0
def test_integration_token():
    response = client.get("/api/auth/integration/token",
                          headers={
                              "Authorization":
                              pytest.token_type + " " + pytest.access_token
                          })

    actual = response.json()
    assert actual["success"]
    assert actual["error_code"] == 0
    assert actual["data"]['access_token']
    assert actual["data"]['token_type']
    assert actual[
        "message"] == '''It is your responsibility to keep the token secret.
        If leaked then other may have access to your system.'''
    response = client.get(
        "/api/bot/intents",
        headers={
            "Authorization":
            actual["data"]['token_type'] + " " +
            actual["data"]['access_token'],
            "X-USER":
            "******"
        },
    )
    actual = response.json()
    assert "data" in actual
    assert len(actual["data"]) == 23
    assert actual["success"]
    assert actual["error_code"] == 0
    assert Utility.check_empty_string(actual["message"])
Example #14
0
 def validate(self, clean=True):
     Utility.validate_document_list(self.events)
     if Utility.check_empty_string(self.block_name):
         raise ValidationError(
             "Story path name cannot be empty or blank spaces")
     elif not self.events:
         raise ValidationError("Stories cannot be empty")
Example #15
0
 def add_bot(name: str, account: int, user: str):
     """ Adds a bot to the specified user account """
     assert not Utility.check_empty_string(name), "Bot Name cannot be empty or blank spaces"
     Utility.is_exist(
         Bot,
         exp_message="Bot already exists!",
         name__iexact=name, account=account, status=True
     )
     return Bot(name=name, account=account, user=user).save().to_mongo().to_dict()
Example #16
0
 def validate(self, clean=True):
     if Utility.check_empty_string(self.name):
         raise ValidationError(
             "Intent Name cannot be empty or blank spaces")
     else:
         if not Utility.special_match(self.name):
             raise ValidationError(
                 "Intent Name must contains alphabets, number and underscore"
             )
Example #17
0
def test_get_training_examples_empty_intent():
    response = client.get(
        "/api/bot/training_examples/ ",
        headers={"Authorization": pytest.token_type + " " + pytest.access_token},
    )
    actual = response.json()
    assert len(actual["data"]) == 0
    assert actual["success"]
    assert actual["error_code"] == 0
    assert Utility.check_empty_string(actual["message"])
Example #18
0
def test_get_user_details():
    response = client.get("/api/user/details",
                           headers={"Authorization": pytest.token_type + " " + pytest.access_token}
                           )

    actual = response.json()
    assert actual["success"]
    assert actual["error_code"] == 0
    assert actual["data"]
    assert Utility.check_empty_string(actual["message"])
Example #19
0
def test_get_utterance_from_not_exist_intent():
    response = client.get(
        "/api/bot/utterance_from_intent/greeting",
        headers={"Authorization": pytest.token_type + " " + pytest.access_token},
    )
    actual = response.json()
    assert actual["success"]
    assert actual["error_code"] == 0
    assert actual["data"] is None
    assert Utility.check_empty_string(actual["message"])
Example #20
0
def test_get_responses():
    response = client.get(
        "/api/bot/response/utter_greet",
        headers={"Authorization": pytest.token_type + " " + pytest.access_token},
    )
    actual = response.json()
    print(actual)
    assert len(actual["data"]) == 1
    assert actual["success"]
    assert actual["error_code"] == 0
    assert Utility.check_empty_string(actual["message"])
Example #21
0
def test_chat_fetch_from_cache():
    response = client.post(
        "/api/bot/chat",
        json={"data": "Hi"},
        headers={"Authorization": pytest.token_type + " " + pytest.access_token},
    )
    actual = response.json()
    assert actual["success"]
    assert actual["error_code"] == 0
    assert actual["data"]
    assert Utility.check_empty_string(actual["message"])
Example #22
0
 def validate(self, clean=True):
     Utility.validate_document_list(self.events)
     if Utility.check_empty_string(self.block_name):
         raise ValidationError(
             "Story path name cannot be empty or blank spaces")
     elif not self.events:
         raise ValidationError("Stories cannot be empty")
     elif self.events[0].type != "user":
         raise ValidationError("Stories must start with intent")
     elif self.events[-1].type != "action":
         raise ValidationError("Stories must end with action")
Example #23
0
 def validate(self, clean=True):
     if Utility.check_empty_string(self.name):
         raise ValidationError(
             "Response name cannot be empty or blank spaces")
     elif not self.text and not self.custom:
         raise ValidationError(
             "Either Text or Custom response must be present!")
     else:
         if self.text:
             self.text.validate()
         elif self.custom:
             self.custom.validate()
Example #24
0
 def validate(self, clean=True):
     if Utility.check_empty_string(self.name) or Utility.check_empty_string(
             self.type):
         raise ValueError(
             "Slot name and type cannot be empty or blank spaces")
     error = ""
     if self.type == FloatSlot.type_name:
         if not self.min_value and not self.max_value:
             self.min_value = 0.0
             self.max_value = 1.0
         if self.min_value < self.max_value:
             error = "FloatSlot must have min_value < max_value"
         if not isinstance(self.value, int):
             if error:
                 error += "\n"
             error = "FloatSlot initial_value must be numeric value"
             ValidationError(error)
     elif self.type == CategoricalSlot.type_name:
         if not self.values:
             raise ValidationError(
                 "CategoricalSlot must have list of categories in values field"
             )
Example #25
0
 def add_training_example(self, examples: List[Text], intent: Text,
                          bot: Text, user: Text):
     if not Utility.is_exist(
             Intents, query={
                 "name": intent,
                 "bot": bot
             }, raise_error=False):
         self.add_intent(intent, bot, user)
     for example in examples:
         if Utility.is_exist(TrainingExamples,
                             query={
                                 "text": example,
                                 "bot": bot
                             },
                             raise_error=False):
             yield {
                 "text": example,
                 "message": "Training Example already exists!",
                 "_id": None,
             }
         else:
             training_example = TrainingExamples(intent=intent,
                                                 text=example,
                                                 bot=bot,
                                                 user=user)
             if not Utility.check_empty_string(example):
                 entities = MarkdownReader._find_entities_in_training_example(
                     example)
                 if entities:
                     ext_entity = [ent["entity"] for ent in entities]
                     self.__save_domain_entities(ext_entity,
                                                 bot=bot,
                                                 user=user)
                     self.__add_slots_from_entities(ext_entity, bot, user)
                     training_example.text = re.sub(
                         ent_regex, lambda m: m.groupdict()["entity_text"],
                         example)
                     training_example.entities = list(
                         self.__extract_entities(entities))
             try:
                 saved = training_example.save().to_mongo().to_dict()
                 yield {
                     "text": example,
                     "_id": saved["_id"].__str__(),
                     "message": "Training Example added successfully!",
                 }
             except Exception as e:
                 yield {"text": example, "_id": None, "message": str(e)}
Example #26
0
def test_integration_token():
    response = client.get(
        "/api/auth/integration/token",
        headers={
            "Authorization": pytest.token_type + " " + pytest.access_token
        },
    )

    token = response.json()
    assert token["success"]
    assert token["error_code"] == 0
    assert token["data"]["access_token"]
    assert token["data"]["token_type"]
    assert (token["message"] ==
            """It is your responsibility to keep the token secret.
        If leaked then other may have access to your system.""")
    response = client.get(
        "/api/bot/intents",
        headers={
            "Authorization":
            token["data"]["token_type"] + " " + token["data"]["access_token"],
            "X-USER":
            "******",
        },
    )
    actual = response.json()
    assert "data" in actual
    assert len(actual["data"]) == 23
    assert actual["success"]
    assert actual["error_code"] == 0
    assert Utility.check_empty_string(actual["message"])
    response = client.post(
        "/api/bot/intents",
        headers={
            "Authorization":
            token["data"]["token_type"] + " " + token["data"]["access_token"],
            "X-USER":
            "******",
        },
        json={"data": "integration"},
    )
    actual = response.json()
    assert actual["data"]["_id"]
    assert actual["success"]
    assert actual["error_code"] == 0
    assert actual["message"] == "Intent added successfully!"
Example #27
0
def test_chat_fetch_from_cache(monkeypatch):
    def mongo_store(*arge, **kwargs):
        return None

    monkeypatch.setattr(Utility, "get_local_mongo_store", mongo_store)
    response = client.post(
        "/api/bot/chat",
        json={"data": "Hi"},
        headers={
            "Authorization": pytest.token_type + " " + pytest.access_token
        },
    )
    actual = response.json()
    assert actual["success"]
    assert actual["error_code"] == 0
    assert actual["data"]
    assert Utility.check_empty_string(actual["message"])
Example #28
0
    def add_session_config(
        self,
        bot: Text,
        user: Text,
        id: Text = None,
        sesssionExpirationTime: int = 60,
        carryOverSlots: bool = True,
    ):
        if not Utility.check_empty_string(id):

            session_config = SessionConfigs.objects().get(id=id)
            session_config.sesssionExpirationTime = sesssionExpirationTime
            session_config.carryOverSlots = carryOverSlots
        else:
            if SessionConfigs.objects(bot=bot):
                raise AppException("Session config already exists!")
            session_config = SessionConfigs(
                sesssionExpirationTime=sesssionExpirationTime,
                carryOverSlots=carryOverSlots,
                bot=bot,
                user=user,
            )

        return session_config.save().to_mongo().to_dict()["_id"].__str__()
Example #29
0
 def validate(self, clean=True):
     if Utility.check_empty_string(self.name):
         raise ValidationError(
             "Account Name cannot be empty or blank spaces")
Example #30
0
 def validate(self, clean=True):
     if Utility.check_empty_string(
             self.synonym) or Utility.check_empty_string(self.value):
         raise ValidationError(
             "Synonym name and value cannot be empty or blank spaces")