def test_optin_optout(rf, admin_user): with override_settings(SHUUP_TELEMETRY_ENABLED=True, DEBUG=True): with patch.object(requests, "post", return_value=MockResponse("test")) as requestor: _clear_telemetry_submission() assert not set_opt_out(False) # Not opted out assert not is_opt_out() try_send_telemetry() with pytest.raises(TelemetryNotSent) as ei: try_send_telemetry(raise_on_error=True) # Still gracey assert ei.value.code == "grace" _backdate_installation_key() try_send_telemetry(max_age_hours=72) try_send_telemetry( max_age_hours=None) # Forcibly re-send for the hell of it with pytest.raises(TelemetryNotSent) as ei: try_send_telemetry( raise_on_error=True ) # Don't ignore last-send; shouldn't send anyway assert ei.value.code == "age" assert len(requestor.mock_calls) == 2 assert set_opt_out(True) assert is_opt_out() with pytest.raises(TelemetryNotSent) as ei: try_send_telemetry(max_age_hours=0, raise_on_error=True) assert ei.value.code == "optout" assert len(requestor.mock_calls) == 2
def test_optin_optout(rf, admin_user): with override_settings(SHUUP_TELEMETRY_ENABLED=True, DEBUG=True): with patch.object(requests, "post", return_value=MockResponse("test")) as requestor: _clear_telemetry_submission() assert not set_opt_out(False) # Not opted out assert not is_opt_out() try_send_telemetry() with pytest.raises(TelemetryNotSent) as ei: try_send_telemetry(raise_on_error=True) # Still gracey assert ei.value.code == "grace" _backdate_installation_key() try_send_telemetry(max_age_hours=72) try_send_telemetry(max_age_hours=None) # Forcibly re-send for the hell of it with pytest.raises(TelemetryNotSent) as ei: try_send_telemetry(raise_on_error=True) # Don't ignore last-send; shouldn't send anyway assert ei.value.code == "age" assert len(requestor.mock_calls) == 2 assert set_opt_out(True) assert is_opt_out() with pytest.raises(TelemetryNotSent) as ei: try_send_telemetry(max_age_hours=0, raise_on_error=True) assert ei.value.code == "optout" assert len(requestor.mock_calls) == 2
def test_disable(rf, admin_user): with override_settings(SHUUP_TELEMETRY_ENABLED=False): _clear_telemetry_submission() _backdate_installation_key() set_opt_out(False) with pytest.raises(TelemetryNotSent) as ei: try_send_telemetry(raise_on_error=True, max_age_hours=None) # Should re-send (if we weren't disabled) assert ei.value.code == "disabled"
def test_graceful_error(admin_user): def thrower(*args, **kwargs): raise ValueError("aaaagh") with override_settings(SHUUP_TELEMETRY_ENABLED=True): with patch.object(requests, "post", thrower) as requestor: _clear_telemetry_submission() _backdate_installation_key() set_opt_out(False) assert try_send_telemetry(raise_on_error=True).get("error")
def save(self): if not self.is_valid(): return opt_in_telemetry = not self.cleaned_data.get("opt_in_telemetry", False) telemetry.set_opt_out(opt_in_telemetry)
def post(self, request, *args, **kwargs): opt = request.POST.get("opt") if opt: telemetry.set_opt_out(opt == "out") return HttpResponseRedirect(request.path)