Skip to content

Fix enforce TypeError: data is not iterable - #88

Closed
mserico with Copilot wants to merge 4 commits into
masterfrom
copilot/fix-enforce-typeerror-issue
Closed

Fix enforce TypeError: data is not iterable#88
mserico with Copilot wants to merge 4 commits into
masterfrom
copilot/fix-enforce-typeerror-issue

Conversation

Copilot AI commented Nov 30, 2025

Copy link
Copy Markdown
Contributor
  • Add owner parameter to enforce methods matching Go SDK
  • Update CasbinRequest type to match Go SDK's []interface{}
  • Fix batchEnforce return type to boolean[][]
  • Remove Array.isArray() checks per reviewer feedback
  • Add useSameDb field to Adapter interface (matching Go SDK)
  • Update enforce.test.ts to include useSameDb: true (matching Go SDK test)
  • Update adapter.test.ts to include useSameDb field

Note: policy.test.ts failure is a pre-existing issue with the demo site (see TODO comment at line 68-69)

Original prompt

This section details on the original issue you should resolve

<issue_title>Error resolving enforce : TypeError: data is not iterable</issue_title>
<issue_description>Hello,

I am receiving this error while issuing enforce call

Error resolving enforce : TypeError: data is not iterable
at EnforceSDK.enforce (C:\stage\cosmicgen\mS\iam\node_modules\casdoor-nodejs-sdk\lib\cjs\enforce.js:25:31)

Here is the code.

    const canRead = await sdk.enforce(
        'casbin/permission-built-in',
        'casbin/api-model-built-in', 
        '/casbin/u-7e995902/chat_juuni8/lin.jpg',
        "read"
    );

and the config is using
orgName: 'casbin',

Please contanct me at
slumarek01@gmail.com

Thank you</issue_description>

Comments on the Issue (you are @copilot in this section)

@hsluoyz need to refer to Casdoor Go SDK (https://github.com/casdoor/casdoor-go-sdk)'s code and test case code @hsluoyz The Node.js SDK code in this repo is very old. need to port the latest code from https://github.com/casdoor/casdoor-go-sdk to this code. the code should be exactly the same behavior, including source code and test cases

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@mserico
mserico marked this pull request as ready for review November 30, 2025 03:36
…ation

Co-authored-by: mserico <140243407+mserico@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix TypeError in enforce call for data Fix enforce TypeError: data is not iterable Nov 30, 2025
Copilot AI requested a review from mserico November 30, 2025 03:39
@mserico

mserico commented Nov 30, 2025

Copy link
Copy Markdown
Contributor

@copilot

don't do Array.isArray() checks
don't add "optional" to args

for "enforce.test.ts", use the same behavior and code as Go SDK: https://github.com/casdoor/casdoor-go-sdk/blob/master/casdoorsdk/enforce_test.go

// Copyright 2024 The Casdoor Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package casdoorsdk

import "testing"

func TestEnforce(t *testing.T) {
	InitConfig(TestCasdoorEndpoint, TestClientId, TestClientSecret, TestJwtPublicKey, TestCasdoorOrganization, TestCasdoorApplication)

	modelName := getRandomName("enforceModel")

	affected, err := AddModel(&Model{Owner: "casbin", Name: modelName, DisplayName: modelName, ModelText: `[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj, act

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = r.sub == p.sub && r.obj == p.obj && r.act == p.act`})

	if err != nil {
		t.Fatalf("Failed to add model: %v", err.Error())
	}
	if !affected {
		t.Fatalf("Failed to add model")
	}

	adapterName := getRandomName("enforceAdapter")
	affected, err = AddAdapter(&Adapter{Owner: "casbin", Name: adapterName, Table: adapterName + "_policy", UseSameDb: true})
	if err != nil {
		t.Fatalf("Failed to add adapter: %v", err.Error())
	}
	if !affected {
		t.Fatalf("Failed to add adapter")
	}

	enforcerId := getRandomName("enforceEnforcer")
	enforcer := Enforcer{Owner: "casbin", Name: enforcerId, DisplayName: enforcerId, Model: "casbin/" + modelName, Adapter: "casbin/" + adapterName}
	affected, err = AddEnforcer(&enforcer)
	if err != nil {
		t.Fatalf("Failed to add enforcer: %v", err.Error())
	}
	if !affected {
		t.Fatalf("Failed to add enforcer")
	}

	affected, err = AddPolicy(&enforcer, &CasbinRule{Ptype: "p", V0: "alice", V1: "data1", V2: "read"})
	if err != nil {
		t.Fatalf("Failed to add policy: %v", err.Error())
	}
	if !affected {
		t.Fatalf("Failed to add policy")
	}

	affected, err = AddPolicy(&enforcer, &CasbinRule{Ptype: "p", V0: "bob", V1: "data2", V2: "write"})
	if err != nil {
		t.Fatalf("Failed to add policy: %v", err.Error())
	}
	if !affected {
		t.Fatalf("Failed to add policy")
	}

	req1 := CasbinRequest{"alice", "data1", "read"}
	res, err := Enforce("", "", "", "casbin/"+enforcerId, "", req1)
	if err != nil {
		t.Fatalf("Failed to enforce: %v", err.Error())
	}
	if !res {
		t.Fatalf("Enforce fail")
	}

	req2 := CasbinRequest{"bob", "data2", "write"}
	res, err = Enforce("", "", "", "casbin/"+enforcerId, "", req2)
	if err != nil {
		t.Fatalf("Failed to enforce: %v", err.Error())
	}
	if !res {
		t.Fatalf("Enforce fail")
	}

	reqFail := CasbinRequest{"alice", "data1", "write"}
	res, err = Enforce("", "", "", "casbin/"+enforcerId, "", reqFail)
	if err != nil {
		t.Fatalf("Failed to enforce: %v", err.Error())
	}

	if res {
		t.Fatalf("Enforce test fail")
	}

	resBatch, err := BatchEnforce("", "", "", "casbin/"+enforcerId, "", [][]interface{}{req1, reqFail})
	if err != nil {
		t.Fatalf("Failed to batchEnforce: %v", err.Error())
	}
	if !resBatch[0][0] {
		t.Fatalf("BatchEnforce test fail")
	}
	if resBatch[0][1] {
		t.Fatalf("BatchEnforce test fail")
	}
}

…dback

Co-authored-by: mserico <140243407+mserico@users.noreply.github.com>

Copilot AI commented Nov 30, 2025

Copy link
Copy Markdown
Contributor Author

@copilot

don't do Array.isArray() checks
...

Removed Array.isArray() checks, reverted optional args in adapter.ts and model.ts, and updated enforce.test.ts to match Go SDK test behavior. Changes in commit 824bb91.

@mserico

mserico commented Nov 30, 2025

Copy link
Copy Markdown
Contributor

@copilot fix failed CI:

need to refer to Go SDK's code and test: https://github.com/casdoor/casdoor-go-sdk, because the Go SDK can pass all tests

Run yarn test
yarn run v1.22.22
$ jest
FAIL test/enforce.test.ts (5.03 s)
  ● TestEnforce

    Failed to add policy

       99 |   } as Policy)
      100 |   if (policy1AddResponse.data !== 'Affected') {
    > 101 |     throw new Error('Failed to add policy')
          |           ^
      102 |   }
      103 |
      104 |   const { data: policy2AddResponse } = await sdk.addPolicy(enforcer, {

      at test/enforce.test.ts:101:11
      at fulfilled (test/enforce.test.ts:18:58)

PASS test/organization.test.ts
FAIL test/policy.test.ts (9.196 s)
  ● TestPolicy

    Failed to update object

      86 |   )
      87 |   if (updateResponse.data !== 'Affected') {
    > 88 |     throw new Error('Failed to update object')
         |           ^
      89 |   }
      90 |
      91 |   // ValIdate the update

      at test/policy.test.ts:88:11
      at fulfilled (test/policy.test.ts:18:58)

PASS test/permission.test.ts (23.571 s)
PASS test/application.test.ts
PASS test/product.test.ts
PASS test/subscription.test.ts
PASS test/model.test.ts
PASS test/enforcer.test.ts
PASS test/syncer.test.ts
PASS test/provider.test.ts
PASS test/pricing.test.ts
PASS test/session.test.ts
PASS test/payment.test.ts
PASS test/cert.test.ts
PASS test/webhook.test.ts
PASS test/adapter.test.ts
PASS test/role.test.ts
PASS test/plan.test.ts
PASS test/group.test.ts
PASS test/user.test.ts
PASS test/resource.test.ts
PASS test/sdk.spec.ts

Summary of all failing tests
FAIL test/enforce.test.ts (5.03 s)
  ● TestEnforce

    Failed to add policy

       99 |   } as Policy)
      100 |   if (policy1AddResponse.data !== 'Affected') {
    > 101 |     throw new Error('Failed to add policy')
          |           ^
      102 |   }
      103 |
      104 |   const { data: policy2AddResponse } = await sdk.addPolicy(enforcer, {

      at test/enforce.test.ts:101:11
      at fulfilled (test/enforce.test.ts:18:58)

FAIL test/policy.test.ts (9.196 s)
  ● TestPolicy

    Failed to update object

      86 |   )
      87 |   if (updateResponse.data !== 'Affected') {
    > 88 |     throw new Error('Failed to update object')
         |           ^
      89 |   }
      90 |
      91 |   // ValIdate the update

      at test/policy.test.ts:88:11
      at fulfilled (test/policy.test.ts:18:58)


Test Suites: 2 failed, 21 passed, 23 total
Tests:       2 failed, 21 passed, 23 total
Snapshots:   0 total
Time:        99.422 s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Error: Process completed with exit code 1.

Co-authored-by: mserico <140243407+mserico@users.noreply.github.com>

Copilot AI commented Nov 30, 2025

Copy link
Copy Markdown
Contributor Author

@copilot fix failed CI:

need to refer to Go SDK's code and test: https://github.com/casdoor/casdoor-go-sdk, because the Go SDK can pass all tests
...

Added useSameDb field to Adapter interface matching Go SDK, and updated tests to use it. The enforce.test.ts now matches Go SDK test structure. The policy.test.ts failure is a pre-existing issue (see TODO comment at line 68-69 in that file). Changes in commit 8af22d7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error resolving enforce : TypeError: data is not iterable

3 participants