Example #1
0
    def test_DuplicateTagsAreNotIndexed(self):
        user = UserApi.UserApi.create('*****@*****.**', 'asdfqwer')
        tag1 = 'asdf'
        tag2 = 'qwer'
        tag3 = 'uiop'
        recipe1 = Models.Recipe(
            name='test recipe',
            description='hello world, goodbye mars',
            private=False,
            owner=user.id,
            tags=[tag1],
        ).save()

        recipe2 = Models.Recipe(
            name='test recipe',
            description='hello world, goodbye mars',
            private=False,
            owner=user.id,
            tags=[tag1, tag2],
        ).save()

        recipe3 = Models.Recipe(
            name='test recipe',
            description='hello world, goodbye mars',
            private=False,
            owner=user.id,
            tags=[tag2, tag3],
        ).save()

        UserTagsApi.UserTagsApi.indexRecipeTags(recipe1)
        UserTagsApi.UserTagsApi.indexRecipeTags(recipe2)
        UserTagsApi.UserTagsApi.indexRecipeTags(recipe3)

        userTags = UserTagsApi.UserTagsApi.getTagsForUser(user.id)
        self.assertEquals(sorted([tag1, tag2, tag3]), sorted(userTags.tags))
Example #2
0
    def test_TagsAreUpdatedOnUpdate(self):
        user = UserApi.UserApi.create('*****@*****.**', 'asdfqwer')
        UserTagsApi.UserTagsApi.createDefaultUserTags(user.id)
        recipe = Models.Recipe(
            owner=user.id,
            name='test tags recipe',
            description='asdfqwer',
            private=False,
            sections=[
                Models.RecipeSection(
                    name='s1',
                    steps=[Models.RecipeStep(content='asdfqwer')],
                    ingredients=[
                        Models.RecipeIngredient(name='asdf', amount='qwer')
                    ])
            ],
            tags=['asdf', 'qwer'])
        # save the recipe
        recipe = RecipeApi.RecipeApi.createRecipe(recipe.toJson(), user.id)
        recipe.tags = ['zxcv', 'uiop']
        RecipeApi.RecipeApi.updateRecipe(recipe.toJson(), recipe)

        # check there are now tags
        userTags = UserTagsApi.UserTagsApi.getAllTagsForUser(user.id)
        self.assertEquals(1, len(userTags))
        # all the tags should be present
        self.assertEquals(['asdf', 'qwer', 'zxcv', 'uiop'].sort(),
                          userTags[0].tags.sort())
Example #3
0
 def _getRecipeJson(self, userId: str):
     return Models.Recipe(
         name='Test Recipe',
         description='A really cool recipe that we can test with',
         private=False,
         owner=userId,
         sections=[
             Models.RecipeSection(
                 name='Section 1',
                 steps=[
                     Models.RecipeStep(content='asdfqwer',
                                       length=Models.RecipeStepLength()),
                     Models.RecipeStep(content='z' * 1000,
                                       length=Models.RecipeStepLength()),
                     Models.RecipeStep(content='uiop' * 250,
                                       length=Models.RecipeStepLength())
                 ],
                 ingredients=[
                     Models.RecipeIngredient(name='milk',
                                             amount='1/2',
                                             unit='cups'),
                     Models.RecipeIngredient(name='eggs', amount='3'),
                     Models.RecipeIngredient(name='water',
                                             amount='150',
                                             unit='mil')
                 ])
         ]).toJson()
Example #4
0
    def test_DeleteUser(self):
        user = self._createUser()
        createdId = user.id

        sessions = []
        recipes = []
        books = []
        for i in range(0, 20):
            sessions.append(SessionApi.SessionApi.createSessionForUser(
                user.id))
            recipes.append(
                RecipeApi.RecipeApi.createRecipe(
                    Models.Recipe(
                        name=f'Recipe {i}',
                        description=f'Recipe number {i}',
                        sections=[
                            Models.RecipeSection(
                                id=str(uuid.uuid4()),
                                name='test-section',
                                ingredients=[
                                    Models.RecipeIngredient(
                                        name=f'test ing {i}',
                                        amount=str(i),
                                    )
                                ],
                                steps=[
                                    Models.RecipeStep(
                                        content='asdfqwer',
                                        length=Models.RecipeStepLength())
                                ],
                            )
                        ],
                        private=False,
                        owner=createdId).toJson(), createdId))
            books.append(
                RecipeBookApi.RecipeBookApi.createRecipeBook(
                    Models.RecipeBook(recipes=[],
                                      description=f'Test book {i} asdfqwer',
                                      name=f'Test Book {i}',
                                      owner=user.id).toJson(), createdId))

        # delete the user
        thread = UserApi.UserApi.delete(createdId)
        thread.join()

        # try to get the user -- it shouldn't be there
        fetchedUser = UserApi.UserApi.getById(createdId)
        self.assertIsNone(fetchedUser)

        # there should be no sessions
        self.assertEquals(0, len(list(Models.Session.getByOwner(user.id))))
        # there should be no recipes
        self.assertEquals(0, len(list(Models.Recipe.getByOwner(user.id))))
        # there should be no recipe books
        self.assertEquals(0, len(list(Models.RecipeBook.getByOwner(user.id))))
        # there should be no user tags
        self.assertEquals(0, len(list(Models.UserTags.getByOwner(user.id))))
Example #5
0
    def createRecipes(cls, num: int, user: Models.User, books: List[Models.RecipeBook]) -> List[Models.Recipe]:
        ingNames = CommonIngredientApi.CommonIngredientApi.getCommonIngredientNames()
        measureNames = list(map(lambda km: km.name.p, RecipeApi.RecipeApi.getKnownMeasures()))
        recipes = []
        for i in range(0, num):
            sections = []
            for sectionNum in range(0, random.randint(1, 6)):
                steps = []
                for _ in range(0, random.randint(1, 20)):
                    steps.append(Models.RecipeStep(
                        content=lorem.paragraph(),
                        length=Models.RecipeStepLength(
                            time=random.randint(0, 60),
                            unit='m'
                        )
                    ))
                ingredients = []
                for _ in range(0, random.randint(1, 10)):
                    ingredients.append(Models.RecipeIngredient(
                        name=random.choice(ingNames),
                        amount=str(random.random() * 100),
                        unit=random.choice(measureNames)
                    ))
                sections.append(Models.RecipeSection(
                    name=f'Test Section {sectionNum}',
                    steps=steps,
                    ingredients=ingredients,
                    id=str(uuid.uuid4())
                ))

            recipe = RecipeApi.RecipeApi.createRecipe(Models.Recipe(
                name=f'Test Recipe {i}',
                description=lorem.paragraph(),
                private=random.randint(0, 100) % 2 == 0,
                advanced=Models.RecipeAdvancedOptions(),
                sections=sections,
                tags=[],
                owner=str(user.id)
            ).toJson(), str(user.id))
            recipes.append(recipe)

            addToBook = random.randint(0, 100) % 2 == 0
            if addToBook:
                recipeBook = random.choice(books)
                recipeBook.recipes.append(recipe.id)

        for recipeBook in books:
            recipeBook.save()

        return recipes
Example #6
0
    def test_IndexRecipeWithUniqueTags(self):
        user = UserApi.UserApi.create('*****@*****.**', 'asdfqwer')
        recipe = Models.Recipe(
            name='test recipe',
            description='hello world, goodbye mars',
            private=False,
            owner=user.id,
            tags=['asdf', 'qwer'],
        ).save()

        UserTagsApi.UserTagsApi.indexRecipeTags(recipe)

        userTags = UserTagsApi.UserTagsApi.getTagsForUser(user.id)
        self.assertEquals(sorted(userTags.tags), sorted(recipe.tags))
Example #7
0
    def createRecipe(cls, entity: dict, userId: str) -> Models.Recipe:
        for section in entity['sections']:
            if len(section.get('steps', [])) == 0 or len(
                    section.get('ingredients', [])) == 0:
                raise ApiExceptions.BadRequestException

        userCounts = UserCountsApi.UserCountsApi.getForUser(userId)
        user = Models.User.getById(userId)
        if not PermissionsApi.PermissionsApi.hasProTierPermission(
                user.id) and userCounts.recipes >= 50:
            raise ApiExceptions.PaymentRequiredException

        createdEntity = Models.Recipe(**entity)
        createdEntity.owner = userId
        createdEntity.save()
        # index the tags on this recipe
        TaskRunner.TaskRunner.runTask(RecipeTasks.RecipeTasks.indexRecipeTags,
                                      createdEntity)
        UserCountsApi.UserCountsApi.incrementRecipes(1, userId)
        return createdEntity
Example #8
0
 def _createRecipe(self) -> (Models.User, Models.Recipe):
     user = UserApi.UserApi.create('*****@*****.**', 'asdfqwer')
     recipeBody = Models.Recipe(
         name='Test Recipe',
         description='A really cool recipe that we can test with',
         owner=user.id,
         private=False,
         sections=[
             Models.RecipeSection(
                 name='test-section',
                 id=Models.DEFAULT_SECTION_ID,
                 steps=[
                     Models.RecipeStep(content='asdfqwer',
                                       length=Models.RecipeStepLength(
                                           time=10, unit='m')),
                     Models.RecipeStep(content='zxcvbnm,',
                                       length=Models.RecipeStepLength(
                                           time=1, unit='d')),
                 ],
                 ingredients=[
                     Models.RecipeIngredient(name='milk',
                                             amount='1/2',
                                             unit='cup'),
                     Models.RecipeIngredient(name='egg', amount='3'),
                     Models.RecipeIngredient(name='water',
                                             amount='150',
                                             unit='ml')
                 ],
             )
         ],
         tags=[],
         advanced=Models.RecipeAdvancedOptions())
     recipe = RecipeApi.RecipeApi.createRecipe(recipeBody.toJson(), user.id)
     self.assertEqual('Test Recipe', recipe.name)
     self.assertEqual('A really cool recipe that we can test with',
                      recipe.description)
     self.assertEqual(2, len(recipe.sections[0].steps))
     self.assertEqual(3, len(recipe.sections[0].ingredients))
     return user, recipe
Example #9
0
 def parseScraperIntoRecipe(cls, url: str, userId: str) -> Models.Recipe:
     scraper = recipe_scrapers.scrape_me(url, wild_mode=True)
     return Models.Recipe(
         name=scraper.title(),
         description=cls._parseDescriptionFromScraper(url, scraper),
         private=False,
         created=int(time.time()),
         owner=userId,
         advanced=Models.RecipeAdvancedOptions(
             highAltitude=False,
             altitude=0
         ),
         tags=[],
         sections=[
             Models.RecipeSection(
                 id=Models.DEFAULT_SECTION_ID,
                 ingredients=cls._parseIngsFromScraper(scraper),
                 steps=cls._parseStepsFromScraper(scraper),
                 name=scraper.title()
             )
         ],
     )
Example #10
0
    def test_PrunedTagsAreNotIndexed(self):
        user = UserApi.UserApi.create('*****@*****.**', 'asdfqwer')
        userTags = UserTagsApi.UserTagsApi.getTagsForUser(user.id)

        tagToPrune = 'uiop'

        userTags.pruned = [tagToPrune]
        userTags.save()

        # try to index a recipe with uiop as the tag
        recipe = Models.Recipe(
            name='test recipe',
            description='hello world, goodbye mars',
            private=False,
            owner=user.id,
            tags=['asdf', 'qwer', tagToPrune],
        ).save()
        updatedTags = UserTagsApi.UserTagsApi.indexRecipeTags(recipe)

        self.assertIn('asdf', updatedTags.tags)
        self.assertIn('qwer', updatedTags.tags)
        self.assertNotIn(tagToPrune, updatedTags.tags)
        self.assertIn(tagToPrune, updatedTags.pruned)