Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(open-api): get authorized apps #3647

Merged

Conversation

Anilople
Copy link
Contributor

What's the purpose of this PR

Let open api know that which applications it can operate.

Which issue(s) this PR fixes:

Relate to #3607

Brief changelog

Add a new controller in portal

Add a new method in ApolloOpenApiClient

Follow this checklist to help us incorporate your contribution quickly and easily:

  • Read the Contributing Guide before making this pull request.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Write necessary unit tests to verify the code.
  • Run mvn clean test to make sure this pull request doesn't break anything.

@codecov-commenter
Copy link

codecov-commenter commented Apr 24, 2021

Codecov Report

❗ No coverage uploaded for pull request base (master@9a6f8e2). Click here to learn what that means.
The diff coverage is 55.43%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master    #3647   +/-   ##
=========================================
  Coverage          ?   50.12%           
  Complexity        ?     2442           
=========================================
  Files             ?      479           
  Lines             ?    14810           
  Branches          ?     1504           
=========================================
  Hits              ?     7424           
  Misses            ?     6865           
  Partials          ?      521           
Impacted Files Coverage Δ
...k/apollo/adminservice/AdminServiceApplication.java 0.00% <ø> (ø)
...mework/apollo/adminservice/ServletInitializer.java 0.00% <ø> (ø)
...o/adminservice/aop/NamespaceAcquireLockAspect.java 70.17% <ø> (ø)
.../apollo/adminservice/controller/AppController.java 86.20% <ø> (ø)
...dminservice/controller/AppNamespaceController.java 34.61% <ø> (ø)
...llo/adminservice/controller/ClusterController.java 56.00% <ø> (ø)
...ollo/adminservice/controller/CommitController.java 60.00% <ø> (ø)
...pollo/adminservice/controller/IndexController.java 50.00% <ø> (ø)
...inservice/controller/InstanceConfigController.java 92.77% <ø> (ø)
...llo/adminservice/controller/ItemSetController.java 100.00% <ø> (ø)
... and 124 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9a6f8e2...5a999dc. Read the comment docs.

@Anilople Anilople added area/openapi apollo-openapi feature Categorizes issue as related to a new feature. labels Apr 24, 2021
Copy link
Member

@nobodyiam nobodyiam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

besides, it's better to add necessary unit tests for the new logic

@github-actions
Copy link

github-actions bot commented May 15, 2021

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@nobodyiam nobodyiam added this to the 1.9.0 milestone May 23, 2021
@JaredTan95
Copy link
Member

as #3747 merged, please update changs log. :-)

@nobodyiam
Copy link
Member

@Anilople any update on this?

@Anilople
Copy link
Contributor Author

besides, it's better to add necessary unit tests for the new logic

Is there a better way to test it? It is a little hard, I don't know how to write test for it.

@nobodyiam
Copy link
Member

besides, it's better to add necessary unit tests for the new logic

Is there a better way to test it? It is a little hard, I don't know how to write test for it.

I notice you added an integration test in com.ctrip.framework.apollo.openapi.v1.controller.AppControllerTest, this looks okay to me. However, in integration test, normally we wouldn't mock the main logic and would set up the data instead, you may refer the example com.ctrip.framework.apollo.configservice.integration.ConfigControllerIntegrationTest for more details.
And in this case, I think a simple unit test is also okay, e.g. after mocking the necessary dependencies, we simple call appController.findAppsAuthorized and verify the execution logic and result. There is also an example for your reference: com.ctrip.framework.apollo.configservice.controller.ConfigControllerTest.

BTW, as @JaredTan95 mentioned, please also update the CHANGES.md.

@Anilople Anilople requested a review from nobodyiam June 20, 2021 02:26
private ConsumerService consumerService;

@Test
@Sql(scripts = "ConsumerServiceIntegrationTest.testFindAppIdsAuthorizedByConsumerId.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
Copy link
Contributor Author

@Anilople Anilople Jun 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dump the part of data as sql from portaldb. Is it a good way to prepare data for test?

Copy link
Member

@nobodyiam nobodyiam Jun 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The data looks ok, however I have some suggestions:

  1. The delete statements are not necessary since they will be cleared via /sql/cleanup.sql
  2. To test the logic, it's better to add more apps, e.g. consumer-test-app-id-0, consumer-test-app-id-1 and consumer-test-app-id-2. And we setup the data that the consumer only has permission to consumer-test-app-id-0 and consumer-test-app-id-1.
  3. For integration test, I would suggest we test against the open api /apps/authorized, e.g using a rest template to request the path
  4. Then the AppController#findAppsAuthorized and ConsumerService#findAppIdsAuthorizedByConsumerId could be tested by pure unit tests, which doesn't need to start a server

public void testFindAppsAuthorized() {
final String token = "3c16bf5b1f44b465179253442460e8c0ad845289";
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set(HttpHeaders.AUTHORIZATION, token);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it ok to set token by this way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's ok

btw, is the following sql necessary? I think every test should clean its data after execution, so there is no need to clear the data before execution?

@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we delete the cleanup before run this test, code chagne to

  @Test
//  @Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
  @Sql(scripts = "/com/ctrip/framework/apollo/openapi/service/ConsumerServiceIntegrationTest.testFindAppIdsAuthorizedByConsumerId.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
  @Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
  public void testFindAppsAuthorized() {

and run a single test AppControllerIntegrationTest, will meet error org.h2.jdbc.JdbcSQLException: Unique index or primary key violation: "PRIMARY KEY ON PUBLIC.PERMISSION(ID)";

full log as follow

org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #12 of class path resource [com/ctrip/framework/apollo/openapi/service/ConsumerServiceIntegrationTest.testFindAppIdsAuthorizedByConsumerId.sql]: INSERT INTO `Permission` (`Id`, `PermissionType`, `TargetId`, `DataChange_CreatedBy`, `DataChange_LastModifiedBy`) VALUES (1, 'AssignRole', 'consumer-test-app-id-0', 'apollo', 'apollo'); nested exception is org.h2.jdbc.JdbcSQLException: Unique index or primary key violation: "PRIMARY KEY ON PUBLIC.PERMISSION(ID)"; SQL statement:
INSERT INTO `Permission` (`Id`, `PermissionType`, `TargetId`, `DataChange_CreatedBy`, `DataChange_LastModifiedBy`) VALUES (1, 'AssignRole', 'consumer-test-app-id-0', 'apollo', 'apollo') [23505-191]

	at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:622)
	at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.populate(ResourceDatabasePopulator.java:254)
	at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:49)
	at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.execute(ResourceDatabasePopulator.java:269)
	at org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener.lambda$executeSqlScripts$4(SqlScriptsTestExecutionListener.java:278)
	at org.springframework.transaction.support.TransactionOperations.lambda$executeWithoutResult$0(TransactionOperations.java:68)
	at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140)
	at org.springframework.transaction.support.TransactionOperations.executeWithoutResult(TransactionOperations.java:67)
	at org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener.executeSqlScripts(SqlScriptsTestExecutionListener.java:278)
	at org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener.lambda$executeSqlScripts$0(SqlScriptsTestExecutionListener.java:200)
	at java.lang.Iterable.forEach(Iterable.java:75)
	at org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener.executeSqlScripts(SqlScriptsTestExecutionListener.java:200)
	at org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener.executeSqlScripts(SqlScriptsTestExecutionListener.java:144)
	at org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener.beforeTestMethod(SqlScriptsTestExecutionListener.java:117)
	at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:289)
	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:221)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: org.h2.jdbc.JdbcSQLException: Unique index or primary key violation: "PRIMARY KEY ON PUBLIC.PERMISSION(ID)"; SQL statement:
INSERT INTO `Permission` (`Id`, `PermissionType`, `TargetId`, `DataChange_CreatedBy`, `DataChange_LastModifiedBy`) VALUES (1, 'AssignRole', 'consumer-test-app-id-0', 'apollo', 'apollo') [23505-191]
	at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
	at org.h2.message.DbException.get(DbException.java:179)
	at org.h2.message.DbException.get(DbException.java:155)
	at org.h2.mvstore.db.MVPrimaryIndex.add(MVPrimaryIndex.java:137)
	at org.h2.mvstore.db.MVTable.addRow(MVTable.java:704)
	at org.h2.command.dml.Insert.insertRows(Insert.java:156)
	at org.h2.command.dml.Insert.update(Insert.java:114)
	at org.h2.command.CommandContainer.update(CommandContainer.java:98)
	at org.h2.command.Command.executeUpdate(Command.java:258)
	at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:184)
	at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:158)
	at com.zaxxer.hikari.pool.ProxyStatement.execute(ProxyStatement.java:95)
	at com.zaxxer.hikari.pool.HikariProxyStatement.execute(HikariProxyStatement.java)
	at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:601)
	... 35 more

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, there is an initialization logic in com.ctrip.framework.apollo.portal.spi.defaultimpl.DefaultRoleInitializationService#initCreateAppRole which will create one set of role, permission and role permission record. I think we could change the id in the test sql.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, is it better to organize the sql file into the sql folder?

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have been moved to folder resources/sql/openapi

nobodyiam
nobodyiam previously approved these changes Jun 26, 2021
Copy link
Member

@nobodyiam nobodyiam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@nobodyiam nobodyiam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@nobodyiam nobodyiam merged commit f1eca02 into apolloconfig:master Jun 26, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Jun 26, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area/openapi apollo-openapi feature Categorizes issue as related to a new feature.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants