There is a correct way to do it.
First, you should never touch/work with your test set while training your model. The test set shall be used to assess your final model's performance. Thus, you should "forget" about it and treat is as unseen data.
Hence, when you fill your missing values in the test set, you can use the mean, median, etc. from the train set. If your train set is reasonably constructed and not biased (e.g. toward single class) - then this data imputation method for the test set will be fairly ok.
To answer your question: No, not both are ok but only one way is ok.
I think if you read more about data imputation, it will help you to understand more about the topic.
There is a lot of research going on in data imputation for missing values. Here, it is valuable to know, that researchers differentiate between different types of missing values:
- Missing completely at random (MCAR)
- Missing at random (MAR)
- Missing not at random (MNAR)
It is important to differentiate between these cases by understanding the source of missingness before deciding what method to use.
Some pointers for you: