def test_it_sends_metrics(self, settings, mocked_autograph): # 3 to sign RecipeFactory.create_batch(3, approver=UserFactory(), enabler=UserFactory(), signed=False) # and 1 to unsign RecipeFactory(approver=UserFactory(), signed=True, enabled=False) with MetricsMock() as mm: call_command("update_recipe_signatures") mm.print_records() assert mm.has_record(GAUGE, stat="normandy.signing.recipes.signed", value=3) assert mm.has_record(GAUGE, stat="normandy.signing.recipes.unsigned", value=1)
def test_recipes_used_by(self): recipe = RecipeFactory(enabled=True) assert [recipe] == list(recipe.action.recipes_used_by) action = ActionFactory() recipes = RecipeFactory.create_batch(2, action=action, enabled=True) assert set(action.recipes_used_by) == set(recipes)
def test_recipes_used_by(self): recipe = RecipeFactory(enabled=True) assert [recipe] == list(recipe.action.recipes_used_by) action = ActionFactory() recipes = RecipeFactory.create_batch(2, action=action, enabled=True) assert set(action.recipes_used_by) == set(recipes)
def test_update_signatures(self, mocker, mock_logger): # Make sure the test environment is clean. This test is invalid otherwise. assert Recipe.objects.all().count() == 0 # Mock the Autographer mock_autograph = mocker.patch('normandy.recipes.models.Autographer') mock_autograph.return_value.sign_data.return_value = [ { 'signature': 'fake signature 1' }, { 'signature': 'fake signature 2' }, ] # Make and sign two recipes (recipe1, recipe2) = RecipeFactory.create_batch(2) Recipe.objects.all().update_signatures() # Assert that the signature update is logged. mock_logger.info.assert_called_with( Whatever.contains(str(recipe1.id), str(recipe2.id)), extra={ 'code': INFO_REQUESTING_RECIPE_SIGNATURES, 'recipe_ids': Whatever.contains(recipe1.id, recipe2.id) }) # Assert the autographer was used as expected assert mock_autograph.called assert mock_autograph.return_value.sign_data.called_with( [Whatever(), Whatever()]) signatures = list(Recipe.objects.all().values_list( 'signature__signature', flat=True)) assert signatures == ['fake signature 1', 'fake signature 2']
def test_update_signatures(self, mocker): # Make sure the test environment is clean. This test is invalid otherwise. assert Recipe.objects.all().count() == 0 # Mock the Autographer mock_autograph = mocker.patch('normandy.recipes.models.Autographer') mock_autograph.return_value.sign_data.return_value = [ {'signature': 'fake signature 1'}, {'signature': 'fake signature 2'}, ] # Make and sign two recipes RecipeFactory.create_batch(2) Recipe.objects.all().update_signatures() # Assert the autographer was used as expected assert mock_autograph.called assert mock_autograph.return_value.sign_data.called_with([Whatever(), Whatever()]) signatures = list(Recipe.objects.all().values_list('signature__signature', flat=True)) assert signatures == ['fake signature 1', 'fake signature 2']
def test_recipes_used_by(self): approver = UserFactory() enabler = UserFactory() recipe = RecipeFactory(approver=approver, enabler=enabler) assert [recipe] == list(recipe.approved_revision.action.recipes_used_by) action = ActionFactory() recipes = RecipeFactory.create_batch(2, action=action, approver=approver, enabler=enabler) assert set(action.recipes_used_by) == set(recipes)
def test_recipes_used_by(self): approver = UserFactory() enabler = UserFactory() recipe = RecipeFactory(approver=approver, enabler=enabler) assert [recipe] == list(recipe.action.recipes_used_by) action = ActionFactory() recipes = RecipeFactory.create_batch(2, action=action, approver=approver, enabler=enabler) assert set(action.recipes_used_by) == set(recipes)
def test_it_does_not_send_excessive_remote_settings_traffic( self, mocker, settings, mocked_autograph ): # 10 to update recipes = RecipeFactory.create_batch( 10, approver=UserFactory(), enabler=UserFactory(), signed=False ) assert all(not r.approved_revision.uses_only_baseline_capabilities() for r in recipes) # Set up a version of the Remote Settings helper with a mocked out client client_mock = None def rs_with_mocked_client(): nonlocal client_mock assert client_mock is None rs = exports.RemoteSettings() client_mock = mocker.MagicMock() rs.client = client_mock return rs mocker.patch( "normandy.recipes.management.commands.update_recipe_signatures.RemoteSettings", side_effect=rs_with_mocked_client, ) call_command("update_recipe_signatures") # Make sure that our mock was actually used assert client_mock # One signing request to the capabilities collection assert client_mock.patch_collection.mock_calls == [ mocker.call( id=settings.REMOTE_SETTINGS_CAPABILITIES_COLLECTION_ID, data={"status": "to-sign"}, bucket=settings.REMOTE_SETTINGS_WORKSPACE_BUCKET_ID, ) ] # one publish to the capabilities collection per recipe expected_calls = [] for recipe in recipes: expected_calls.append( mocker.call( data=Whatever(lambda r: r["id"] == recipe.id, name=f"Recipe {recipe.id}"), bucket=settings.REMOTE_SETTINGS_WORKSPACE_BUCKET_ID, collection=settings.REMOTE_SETTINGS_CAPABILITIES_COLLECTION_ID, ) ) client_mock.update_record.has_calls(expected_calls, any_order=True) # all expected calls assert client_mock.update_record.call_count == len(expected_calls) # no extra calls
def test_recipes_used_by_empty(self): assert list(ActionFactory().recipes_used_by) == [] action = ActionFactory() RecipeFactory.create_batch(2, action=action, enabled=False) assert list(action.recipes_used_by) == []
def test_recipes_used_by_empty(self): assert list(ActionFactory().recipes_used_by) == [] action = ActionFactory() RecipeFactory.create_batch(2, action=action) assert list(action.recipes_used_by) == []