Skip to content

Commit

Permalink
isFeatureEnabled now returns false if disabled (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto authored Oct 9, 2023
1 parent 4474be1 commit 74d803c
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Next

- isFeatureEnabled now returns false if disabled [#74](https://github.com/PostHog/posthog-ios/pull/74)
- Add loadFeatureFlagsOnStart configuration flag [#71](https://github.com/PostHog/posthog-ios/pull/71)

## 2.0.5 - 2023-10-06
Expand Down
2 changes: 1 addition & 1 deletion Examples/CarthageExample/CarthageExample/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import UIKit
import PostHog

// Use your own apiKey!
let PostHog = PHGPostHog(configuration: PHGPostHogConfiguration(apiKey: "b2XoQDHA5YhWQAjr2FZ4qIckgyEqXyY8"))
let PostHog = PHGPostHog(configuration: PHGPostHogConfiguration(apiKey: "_6SG-F7I1vCuZ-HdJL3VZQqjBlaSb1_20hDPwqMNnGI"))

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
Expand Down
2 changes: 1 addition & 1 deletion Examples/CocoapodsExample/CocoapodsExample/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ @interface AppDelegate ()

@end

NSString *const POSTHOG_API_KEY = @"BrpS4SctoaCCsyjlnlun3OzyNJAafdlv__jUWaaJWXg";
NSString *const POSTHOG_API_KEY = @"_6SG-F7I1vCuZ-HdJL3VZQqjBlaSb1_20hDPwqMNnGI";


@implementation AppDelegate
Expand Down
2 changes: 1 addition & 1 deletion Examples/ManualExample/ManualExample/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ @implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Use your own apiKey!
[PHGPostHog setupWithConfiguration:[PHGPostHogConfiguration configurationWithApiKey:@"St5WbSrxW5bzxn2NGwPDVzS9PBPYctOl" host:@"https://app.posthog.com"]];
[PHGPostHog setupWithConfiguration:[PHGPostHogConfiguration configurationWithApiKey:@"_6SG-F7I1vCuZ-HdJL3VZQqjBlaSb1_20hDPwqMNnGI" host:@"https://app.posthog.com"]];
[[PHGPostHog sharedPostHog] capture:@"Manual Example Launched"];
[[PHGPostHog sharedPostHog] flush];

Expand Down
14 changes: 12 additions & 2 deletions PostHog/Classes/PHGPostHog.m
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,18 @@ - (bool)isFeatureEnabled:(NSString *)flagKey

- (bool)isFeatureEnabled:(NSString *)flagKey options:(NSDictionary *)options
{
NSArray *keys = [self.payloadManager getFeatureFlags];
BOOL isFlagEnabled = [keys containsObject: flagKey];
NSDictionary *flags = [self.payloadManager getFlagVariants];

bool isFlagEnabled = true;
id value = [flags valueForKey:flagKey];

if (value != nil) {
if ([value isKindOfClass:[NSNumber class]]) {
isFlagEnabled = [value boolValue];
}
} else {
isFlagEnabled = false;
}

id send_event = [options valueForKey:@"send_event"];

Expand Down
2 changes: 1 addition & 1 deletion PostHogTests/CapturingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class CapturingTests: QuickSpec {
var posthog: PHGPostHog!

beforeEach {
let config = PHGPostHogConfiguration(apiKey: "QUI5ydwIGeFFTa1IvCBUhxL9PyW5B0jE")
let config = PHGPostHogConfiguration(apiKey: "foobar")
passthrough = PHGPassthroughMiddleware()
config.middlewares = [
passthrough,
Expand Down
3 changes: 1 addition & 2 deletions PostHogTests/EndToEndTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ class PostHogE2ETests: QuickSpec {
var posthog: PHGPostHog!

beforeEach {
// let config = PHGPostHogConfiguration(apiKey: "BrpS4SctoaCCsyjlnlun3OzyNJAafdlv__jUWaaJWXg")
let config = PHGPostHogConfiguration(apiKey: "8jVz0YZ2YPtP7eL1I5l5RQIp-WcuFeD3pZO8c0YDMx4", host: "http://localhost:8000")
let config = PHGPostHogConfiguration(apiKey: "foobar")
config.flushAt = 1

PHGPostHog.setup(with: config)
Expand Down
19 changes: 15 additions & 4 deletions PostHogTests/FeatureFlagTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class FeatureFlagTests: QuickSpec {

beforeEach {
LSNocilla.sharedInstance().start()
let config = PHGPostHogConfiguration(apiKey: "QUI5ydwIGeFFTa1IvCBUhxL9PyW5B0jE", host: "https://app.posthog.test")
let config = PHGPostHogConfiguration(apiKey: "foobar", host: "https://app.posthog.test")
passthrough = PHGPassthroughMiddleware()
config.middlewares = [
passthrough,
Expand All @@ -27,13 +27,24 @@ class FeatureFlagTests: QuickSpec {
it("checks flag is enabled") {
_ = stubRequest("POST", "https://app.posthog.test/decide/?v=3" as LSMatcheable)
.andReturn(200)?
.withBody("{\"featureFlags\":{\"some-flag\":\"true\"}}" as LSHTTPBody);
.withBody("{\"featureFlags\":{\"some-flag\": true}}" as LSHTTPBody);
posthog.reloadFeatureFlags()
// Hacky: Need to buffer for async request to happen without stub being cleaned up
sleep(1)
let isEnabled = posthog.isFeatureEnabled("some-flag")
expect(isEnabled).to(beTrue())
}

it("checks flag is disabled") {
_ = stubRequest("POST", "https://app.posthog.test/decide/?v=3" as LSMatcheable)
.andReturn(200)?
.withBody("{\"featureFlags\":{\"some-flag\": false}}" as LSHTTPBody);
posthog.reloadFeatureFlags()
// Hacky: Need to buffer for async request to happen without stub being cleaned up
sleep(1)
let isEnabled = posthog.isFeatureEnabled("some-flag")
expect(isEnabled).to(beFalse())
}

it("checks multivariate flag is enabled - integer") {
_ = stubRequest("POST", "https://app.posthog.test/decide/?v=3" as LSMatcheable)
Expand Down Expand Up @@ -212,7 +223,7 @@ class FeatureFlagTests: QuickSpec {
it("bad request does not override current flags") {
_ = stubRequest("POST", "https://app.posthog.test/decide/?v=3" as LSMatcheable)
.andReturn(200)?
.withBody("{\"featureFlags\":{\"some-flag\":\"true\"}}" as LSHTTPBody);
.withBody("{\"featureFlags\":{\"some-flag\": true}}" as LSHTTPBody);
posthog.reloadFeatureFlags()
// Hacky: Need to buffer for async request to happen without stub being cleaned up
sleep(1)
Expand All @@ -231,7 +242,7 @@ class FeatureFlagTests: QuickSpec {
it("Won't send $feature_flag_called if option is set to false") {
_ = stubRequest("POST", "https://app.posthog.test/decide/?v=3" as LSMatcheable)
.andReturn(200)?
.withBody("{\"featureFlags\":{\"some-flag\":\"true\"}}" as LSHTTPBody);
.withBody("{\"featureFlags\":{\"some-flag\": true}}" as LSHTTPBody);
posthog.reloadFeatureFlags()
// Hacky: Need to buffer for async request to happen without stub being cleaned up
sleep(1)
Expand Down
6 changes: 3 additions & 3 deletions PostHogTests/MiddlewareTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ let eatAllCalls = PHGBlockMiddleware { (context, next) in
class MiddlewareTests: QuickSpec {
override func spec() {
it("receives events") {
let config = PHGPostHogConfiguration(apiKey: "TESTKEY")
let config = PHGPostHogConfiguration(apiKey: "foobar")
let passthrough = PHGPassthroughMiddleware()
config.middlewares = [
passthrough,
Expand All @@ -43,7 +43,7 @@ class MiddlewareTests: QuickSpec {
}

it("modifies and passes event to next") {
let config = PHGPostHogConfiguration(apiKey: "TESTKEY")
let config = PHGPostHogConfiguration(apiKey: "foobar")
let passthrough = PHGPassthroughMiddleware()
config.middlewares = [
customizeAllCaptureCalls,
Expand All @@ -60,7 +60,7 @@ class MiddlewareTests: QuickSpec {
}

it("expects event to be swallowed if next is not called") {
let config = PHGPostHogConfiguration(apiKey: "TESTKEY")
let config = PHGPostHogConfiguration(apiKey: "foobar")
let passthrough = PHGPassthroughMiddleware()
config.middlewares = [
eatAllCalls,
Expand Down
8 changes: 4 additions & 4 deletions PostHogTests/PostHogTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import PostHog

class PostHogTests: QuickSpec {
override func spec() {
let config = PHGPostHogConfiguration(apiKey: "QUI5ydwIGeFFTa1IvCBUhxL9PyW5B0jE")
let config = PHGPostHogConfiguration(apiKey: "foobar")
var posthog: PHGPostHog!
var testMiddleware: TestMiddleware!
var testApplication: TestApplication!
Expand All @@ -31,7 +31,7 @@ class PostHogTests: QuickSpec {
expect(posthog.configuration.flushAt) == 20
expect(posthog.configuration.flushInterval) == 30
expect(posthog.configuration.maxQueueSize) == 1000
expect(posthog.configuration.apiKey) == "QUI5ydwIGeFFTa1IvCBUhxL9PyW5B0jE"
expect(posthog.configuration.apiKey) == "foobar"
expect(posthog.configuration.host) == URL(string: "https://app.posthog.com")
expect(posthog.configuration.shouldUseLocationServices) == false
expect(posthog.configuration.shouldUseBluetooth) == false
Expand All @@ -55,15 +55,15 @@ class PostHogTests: QuickSpec {
}

it("initialized correctly with api host") {
let config = PHGPostHogConfiguration(apiKey: "QUI5ydwIGeFFTa1IvCBUhxL9PyW5B0jE", host: "https://testapp.posthog.test")
let config = PHGPostHogConfiguration(apiKey: "foobar", host: "https://testapp.posthog.test")
config.libraryName = "posthog-ios-test"
config.libraryVersion = "posthog-ios-version"

posthog = PHGPostHog(configuration: config)
expect(posthog.configuration.flushAt) == 20
expect(posthog.configuration.flushInterval) == 30
expect(posthog.configuration.maxQueueSize) == 1000
expect(posthog.configuration.apiKey) == "QUI5ydwIGeFFTa1IvCBUhxL9PyW5B0jE"
expect(posthog.configuration.apiKey) == "foobar"
expect(posthog.configuration.host) == URL(string: "https://testapp.posthog.test")
expect(posthog.configuration.shouldUseLocationServices) == false
expect(posthog.configuration.shouldUseBluetooth) == false
Expand Down

0 comments on commit 74d803c

Please sign in to comment.