11defmodule Realtime.ApiTest do
2- use Realtime.DataCase , async: false
2+ use Realtime.DataCase , async: true
33
44 use Mimic
55
@@ -13,22 +13,24 @@ defmodule Realtime.ApiTest do
1313
1414 @ db_conf Application . compile_env ( :realtime , Realtime.Repo )
1515
16- setup do
17- tenant1 = Containers . checkout_tenant ( run_migrations: true )
18- tenant2 = Containers . checkout_tenant ( run_migrations: true )
19- Api . update_tenant ( tenant1 , % { max_concurrent_users: 10_000_000 } )
20- Api . update_tenant ( tenant2 , % { max_concurrent_users: 20_000_000 } )
21-
22- % { tenants: Api . list_tenants ( ) , tenant: tenant1 }
16+ defp create_tenants ( _ ) do
17+ tenant1 = tenant_fixture ( % { max_concurrent_users: 10_000_000 } )
18+ tenant2 = tenant_fixture ( % { max_concurrent_users: 20_000_000 } )
19+ dev_tenant = Realtime.Api . get_tenant_by_external_id ( "dev_tenant" )
20+ % { tenants: [ tenant1 , tenant2 , dev_tenant ] }
2321 end
2422
2523 describe "list_tenants/0" do
24+ setup [ :create_tenants ]
25+
2626 test "returns all tenants" , % { tenants: tenants } do
2727 assert Enum . sort ( Api . list_tenants ( ) ) == Enum . sort ( tenants )
2828 end
2929 end
3030
3131 describe "list_tenants/1" do
32+ setup [ :create_tenants ]
33+
3234 test "list_tenants/1 returns filtered tenants" , % { tenants: tenants } do
3335 assert hd ( Api . list_tenants ( search: hd ( tenants ) . external_id ) ) == hd ( tenants )
3436
@@ -38,6 +40,8 @@ defmodule Realtime.ApiTest do
3840 end
3941
4042 describe "get_tenant!/1" do
43+ setup [ :create_tenants ]
44+
4145 test "returns the tenant with given id" , % { tenants: [ tenant | _ ] } do
4246 result = tenant . id |> Api . get_tenant! ( ) |> Map . delete ( :extensions )
4347 expected = tenant |> Map . delete ( :extensions )
@@ -90,6 +94,8 @@ defmodule Realtime.ApiTest do
9094 end
9195
9296 describe "get_tenant_by_external_id/1" do
97+ setup [ :create_tenants ]
98+
9399 test "fetch by external id" , % { tenants: [ tenant | _ ] } do
94100 % Tenant { extensions: [ % Extensions { } = extension ] } =
95101 Api . get_tenant_by_external_id ( tenant . external_id )
@@ -101,7 +107,9 @@ defmodule Realtime.ApiTest do
101107 end
102108
103109 describe "update_tenant/2" do
104- test "valid data updates the tenant" , % { tenant: tenant } do
110+ setup [ :create_tenants ]
111+
112+ test "valid data updates the tenant" , % { tenants: [ tenant | _ ] } do
105113 update_attrs = % {
106114 external_id: tenant . external_id ,
107115 jwt_secret: "some updated jwt_secret" ,
@@ -115,35 +123,36 @@ defmodule Realtime.ApiTest do
115123 assert tenant . name == "some updated name"
116124 end
117125
118- test "invalid data returns error changeset" , % { tenant: tenant } do
126+ test "invalid data returns error changeset" , % { tenants: [ tenant | _ ] } do
119127 assert { :error , % Ecto.Changeset { } } = Api . update_tenant ( tenant , % { external_id: nil , jwt_secret: nil , name: nil } )
120128 end
121129
122- test "valid data and jwks change will send disconnect event" , % { tenant: tenant } do
130+ test "valid data and jwks change will send disconnect event" , % { tenants: [ tenant | _ ] } do
123131 :ok = Phoenix.PubSub . subscribe ( Realtime.PubSub , "realtime:operations:" <> tenant . external_id )
124132 assert { :ok , % Tenant { } } = Api . update_tenant ( tenant , % { jwt_jwks: % { keys: [ "test" ] } } )
125133 assert_receive :disconnect , 500
126134 end
127135
128- test "valid data and jwt_secret change will send disconnect event" , % { tenant: tenant } do
136+ test "valid data and jwt_secret change will send disconnect event" , % { tenants: [ tenant | _ ] } do
129137 :ok = Phoenix.PubSub . subscribe ( Realtime.PubSub , "realtime:operations:" <> tenant . external_id )
130138 assert { :ok , % Tenant { } } = Api . update_tenant ( tenant , % { jwt_secret: "potato" } )
131139 assert_receive :disconnect , 500
132140 end
133141
134- test "valid data and suspend change will send disconnect event" , % { tenant: tenant } do
142+ test "valid data and suspend change will send disconnect event" , % { tenants: [ tenant | _ ] } do
135143 :ok = Phoenix.PubSub . subscribe ( Realtime.PubSub , "realtime:operations:" <> tenant . external_id )
136144 assert { :ok , % Tenant { } } = Api . update_tenant ( tenant , % { suspend: true } )
137145 assert_receive :disconnect , 500
138146 end
139147
140- test "valid data but not updating jwt_secret or jwt_jwks won't send event" , % { tenant: tenant } do
148+ test "valid data but not updating jwt_secret or jwt_jwks won't send event" , % { tenants: [ tenant | _ ] } do
141149 :ok = Phoenix.PubSub . subscribe ( Realtime.PubSub , "realtime:operations:" <> tenant . external_id )
142150 assert { :ok , % Tenant { } } = Api . update_tenant ( tenant , % { max_events_per_second: 100 } )
143151 refute_receive :disconnect , 500
144152 end
145153
146- test "valid data and jwt_secret change will restart the database connection" , % { tenant: tenant } do
154+ test "valid data and jwt_secret change will restart the database connection" do
155+ tenant = Containers . checkout_tenant ( run_migrations: true )
147156 { :ok , old_pid } = Connect . lookup_or_start_connection ( tenant . external_id )
148157
149158 Process . monitor ( old_pid )
@@ -155,7 +164,8 @@ defmodule Realtime.ApiTest do
155164 assert % Postgrex.Result { } = Postgrex . query! ( new_pid , "SELECT 1" , [ ] )
156165 end
157166
158- test "valid data and suspend change will restart the database connection" , % { tenant: tenant } do
167+ test "valid data and suspend change will restart the database connection" do
168+ tenant = Containers . checkout_tenant ( run_migrations: true )
159169 { :ok , old_pid } = Connect . lookup_or_start_connection ( tenant . external_id )
160170
161171 Process . monitor ( old_pid )
@@ -166,7 +176,9 @@ defmodule Realtime.ApiTest do
166176 assert { :error , :tenant_suspended } = Connect . lookup_or_start_connection ( tenant . external_id )
167177 end
168178
169- test "valid data and tenant data change will not restart the database connection" , % { tenant: tenant } do
179+ test "valid data and tenant data change will not restart the database connection" do
180+ tenant = Containers . checkout_tenant ( run_migrations: true )
181+ expect ( Realtime.Tenants.Cache , :distributed_invalidate_tenant_cache , fn _ -> :ok end )
170182 { :ok , old_pid } = Connect . lookup_or_start_connection ( tenant . external_id )
171183
172184 assert { :ok , % Tenant { } } = Api . update_tenant ( tenant , % { max_concurrent_users: 100 } )
@@ -176,7 +188,8 @@ defmodule Realtime.ApiTest do
176188 assert old_pid == new_pid
177189 end
178190
179- test "valid data and extensions data change will restart the database connection" , % { tenant: tenant } do
191+ test "valid data and extensions data change will restart the database connection" do
192+ tenant = Containers . checkout_tenant ( run_migrations: true )
180193 config = Realtime.Database . from_tenant ( tenant , "realtime_test" , :stop )
181194
182195 extensions = [
@@ -208,12 +221,12 @@ defmodule Realtime.ApiTest do
208221 assert % Postgrex.Result { } = Postgrex . query! ( new_pid , "SELECT 1" , [ ] )
209222 end
210223
211- test "valid data and change to tenant data will refresh cache" , % { tenant: tenant } do
224+ test "valid data and change to tenant data will refresh cache" , % { tenants: [ tenant | _ ] } do
212225 assert { :ok , % Tenant { } } = Api . update_tenant ( tenant , % { name: "new_name" } )
213226 assert % Tenant { name: "new_name" } = Realtime.Tenants.Cache . get_tenant_by_external_id ( tenant . external_id )
214227 end
215228
216- test "valid data and no changes to tenant will not refresh cache" , % { tenant: tenant } do
229+ test "valid data and no changes to tenant will not refresh cache" , % { tenants: [ tenant | _ ] } do
217230 reject ( & Realtime.Tenants.Cache . distributed_invalidate_tenant_cache / 1 )
218231 assert { :ok , % Tenant { } } = Api . update_tenant ( tenant , % { name: tenant . name } )
219232 end
@@ -237,6 +250,8 @@ defmodule Realtime.ApiTest do
237250 end
238251
239252 describe "preload_counters/1" do
253+ setup [ :create_tenants ]
254+
240255 test "preloads counters for a given tenant " , % { tenants: [ tenant | _ ] } do
241256 tenant = Repo . reload! ( tenant )
242257 assert Api . preload_counters ( nil ) == nil
0 commit comments